| MSBuild-1 :👈 | 👉:MSBuild-3 |
Day 2 – MSBuild Properties and Items |
Today you will learn how MSBuild handles Properties and Items, how they are evaluated, and how transforms, metadata, and batching work. These concepts are the foundation for real-world custom MSBuild logic.
Properties are simple key-value pairs. They always store a single string value and are accessed using
the syntax $(PropertyName).
<PropertyGroup>
<Configuration>Debug</Configuration>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
Examples of built‑in properties:
$(MSBuildProjectDirectory)$(MSBuildProjectName)$(Configuration)$(Platform)Condition attribute controls which value applies.
<PropertyGroup>
<OutputPath Condition=" '$(Configuration)' == 'Debug' ">bin\Debug\</OutputPath>
<OutputPath Condition=" '$(Configuration)' == 'Release' ">bin\Release\</OutputPath>
</PropertyGroup>
Items represent collections/lists of things such as files, resources, or custom entries.
They are accessed using @(ItemName).
<ItemGroup>
<Image Include="images\*.png" />
<Document Include="docs\*.pdf" />
</ItemGroup>
Items may have metadata, used during tasks.
<ItemGroup>
<Image Include="images\logo.png">
<Quality>High</Quality>
</Image>
</ItemGroup>
Metadata is accessed using:
%(Image.Quality)
Transforms allow you to create new item lists based on other items. This is very powerful for automation.
Example: change file extension
@(Images->'%(Filename).webp')
Example: prepend folder
@(Documents->'processed\%(Filename)%(Extension)')
Batching means MSBuild will run a task separately for each item or group of items that share the same metadata.
Example:
<Target Name="ShowImages">
<Message Text="Processing %(Image.Identity) with quality %(Image.Quality)" />
</Target>
If you have 3 images, the task runs 3 times — once per item.
You can mix both in many scenarios.
<Target Name="CopyImages">
<Copy
SourceFiles="@(Image)"
DestinationFolder="$(OutputPath)\images" />
</Target>
Create a new console project and update your .csproj:
<PropertyGroup>
<MyFolder>assets</MyFolder>
</PropertyGroup>
<ItemGroup>
<Asset Include="$(MyFolder)\*.txt" />
</ItemGroup>
<Target Name="ShowAssets">
<Message Text="Found asset: %(Asset.Identity)" />
</Target>
Run:
dotnet build -t:ShowAssets
Tomorrow you will learn:
| MSBuild-1 :👈 | 👉:MSBuild-3 |