In previous posts, I described a few different uses of the MSBUILD Copy task to gather files into the required locations for packaging or testing. While using this method over the last few months, I came across a quirky behavior of MSBUILD in which files added to a project after the build project was open were not getting copied. It turns out that this is "as designed."
I was using the ItemGroup task to specify which files should be copied. Because the group represented by this task is created when the build target file is first loaded, the files in the group were only those that existed when the project was opened. Any files added after were left out of the group.
For a time, I was getting around this by unloading/reloading the project each time I needed to add files. Eventually, I decided to look into the root of the problem and soon discovered that I should have been using the CreateItem task instead of ItemGroup. The difference between these two tasks is that CreateItem is dynamic and will capture the specfied files each time the task is run.
Below is an example of the CreateItem task. When this task executes, it results in the creation of an item called MyProjectRootFiles that contains the files in directory tree starting at the RootFiles folder.
<Output TaskParameter="Include" ItemName="MyProjectRootFiles" />
A CreateItem task should replace each file group item previously defined in the ItemGroup task.