MSBuild properties and targets
MSBuild properties and targets
MSBuild evaluates project files and imports into properties and items, then executes requested targets. A property holds a string value; an item represents an entry in a list and may carry metadata. A target coordinates tasks. Reading evaluated values is different from executing a build.
Proposed baseline: .NET SDK 10.0.404, net10.0 / C# 14. All results are reasoned, not executed. Build and test examples require an approved isolated environment. Framework examples are separate test classes, not console entry points.
Inspect a conditional property
Save as Inspect.proj, a standalone reviewed MSBuild project. The default condition applies only if BuildLabel is empty.
<Project> <PropertyGroup> <BuildLabel Condition="'$(BuildLabel)' == ''">local</BuildLabel> </PropertyGroup> <ItemGroup> <Input Include="alpha.txt" /> <Input Include="beta.txt" /> </ItemGroup> <Target Name="Report"> <Message Text="$(BuildLabel): @(Input, ', ')" Importance="high" /> </Target></Project>MSBuild property references use $(...), item references use @(...). The Input items are just names; this target does not read those files.
Expected observation (reasoned, not executed):
The evaluated BuildLabel is local; running Report prints a message containing local: alpha.txt, beta.txt.Override without editing the project
Run from the folder containing Inspect.proj, using the reviewed project and installed SDK.
dotnet msbuild Inspect.proj -getProperty:BuildLabeldotnet msbuild Inspect.proj -p:BuildLabel=ci -getProperty:BuildLabeldotnet msbuild Inspect.proj -t:Report -p:BuildLabel=ciCommand-line properties are usually global overrides; TreatAsLocalProperty and more complex project graphs can change this. The first two commands evaluate rather than execute targets; evaluation is still not safe for arbitrary untrusted projects.
Expected observation (reasoned, not executed):
First property: local. Second property: ci. Report message contains ci: alpha.txt, beta.txt; tool banners may vary.Choose target order explicitly
Save as Order.proj. The build graph expresses prerequisites independently of the position of target declarations.
<Project> <Target Name="Summary" DependsOnTargets="Prepare"> <Message Text="summary" Importance="high" /> </Target> <Target Name="Prepare"> <Message Text="prepare" Importance="high" /> </Target></Project>DependsOnTargets expresses a dependency. BeforeTargets and AfterTargets are hooks around a named target. Directory.Build.props and Directory.Build.targets customize imported settings; their position matters.
Expected observation (reasoned, not executed):
dotnet msbuild Order.proj -t:Summary prints prepare before summary (apart from tool messages).Common mistakes and edge cases
MSBuild projects can execute tasks, invoke tools, load analyzers and import additional files. Never build a random downloaded project just to inspect it. Do not use absolute file paths or publish build logs containing private configuration. A successful property query does not prove a successful compile. Use the smallest relevant target and inspect inherited configuration.
Practice and recap
Add a second conditional property Channel with default preview and override it to stable using the CLI. Predict the result of -getProperty:Channel.
Hint: Use the same empty-value condition as BuildLabel.
Learning objective: Trace a reviewed property override. See accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
MSBuild properties and targets — knowledge check
Apply the lesson concepts to the following scenarios.
Which notation expands an MSBuild property?
Which construct holds a list of input entries?
What does -getProperty without a requested target inspect?
Which target runs first for Summary DependsOnTargets=Prepare?
Why review imported build files?