Multiple ItemGroup in MSBuild Projects
October 24, 2005 Written by Charles CookBarry's MSBuild session on Saturday was the first time I'd studied an MSBuild project file. I was initially puzzled by the existence of multiple <ItemGroup> elements in a project, for example in a C# project file generated by VS2005 where the references are separate from the source files. I wondered whether this had any significance or was possibly just an artefact of the way the file was generated. After some experimentation it seems that the contents of multiple <ItemGroup> elements are treated as if they were in a single <ItemGroup> element. For example, this project:
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>works in the same way as this project:<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="System" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
Update: Barry posted a follow-up on his blog, describing scenarios in which you could use multiple ItemGroups.
Copyright © 2011, Charles Cook.