msbuild Order of Property and Item Evaluation Example illustrating the order of evaluation

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

MSBuild evaluates PropertyGroup, Choose and ItemGroup elements that are directly under the Project element before those that are in Target elements.

  • Directly under the Project element, PropertyGroup and Choose elements are evaluated in the order in which they appear, and then ItemGroup elements are evaluated in the order in which they appear.
  • In Target elements PropertyGroup and ItemGroup share equal precedence and are evaluated in the order in which they appear.

Within files referenced via Import, MSBuild evaluates PropertyGroup, Choose and ItemGroup in the same manner as above, and as though the imported files' content appeared inline where the Import is located.

The comments below provide property values and item counts before and after MSBuild evaluates selected lines.

<Project DefaultTargets="FooTarget" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="FooTarget">
        <ItemGroup>
            <!-- '$(FooProp)' == '2', '@(FooItem->Count())' == '1' -->
            <FooItem Include="foo value B" />
            <!-- '$(FooProp)' == '2', '@(FooItem->Count())' == '2' -->
        </ItemGroup>
        <PropertyGroup>
            <!-- '$(FooProp)' == '2', '@(FooItem->Count())' == '2' -->
            <FooProp>3</FooProp>
            <!-- '$(FooProp)' == '3', '@(FooItem->Count())' == '2' -->
        </PropertyGroup>
    </Target>
    <ItemGroup>
        <!-- '$(FooProp)' == '2', '@(FooItem->Count())' == '0' -->
        <FooItem Include="foo value A" />
        <!-- '$(FooProp)' == '2', '@(FooItem->Count())' == '1' -->
    </ItemGroup>
    <PropertyGroup>
        <!-- '$(FooProp)' == '', '@(FooItem->Count())' == '0' -->
        <FooProp>1</FooProp>
        <!-- '$(FooProp)' == '1', '@(FooItem->Count())' == '0' -->
    </PropertyGroup>
    <Choose>
        <When Condition=" '$(FooProp)' == '1' ">
            <!-- '$(FooProp)' == '1', '@(FooItem->Count())' == '0' -->
            <FooProp>2</FooProp>
            <!-- '$(FooProp)' == '2', '@(FooItem->Count())' == '0' -->
        </When>
    </Choose>
</Project>


Got any msbuild Question?