Creating MSBuild ItemGroups On The Fly

by DRohm 13. January 2006 21:22

One thing I've noticed while creating MSBuild files is that when MSBuild is launched the ItemGroups are evaluated before Targets are executed.  Here is an example of what that means.  Say you declare an ItemGroup that will contain all the files for a project:

  407   <ItemGroup>

  408     <ProjectFiles Include="$(MSBuildProjectDirectory)\MyProject\**\*.*" />

  409   </ItemGroup>

ProjectFiles will only contain all of the files in the project folder before the build, not any of the post compilation files. What you need to do in this instance is create the ItemGroup on the fly like so:

  398     <CreateItem Include="$(MSBuildProjectDirectory)\MyProject\**\*.*">

  399       <Output TaskParameter="Include" ItemName="ProjectFiles" />

  400     </CreateItem>

Place this task inside the target where you need the ItemGroup.  After this task executes, you will now have an ItemGroup that contains all the files in the project directory, including the files produced by compilation.

Tags:

blog comments powered by Disqus

About Me

Doug Rohm
Twitter Technorati

Doug Rohm
Boston area .NET developer, geek, gamer, dog lover, and sports nut.

Windows Phone 7 Developer Launch