MSBuild

  • Runs build processes to build apps.
  • Used by Visual Studio to build.
  • Used by dotnet build to build.

Properties

Key-value pairs used to configure builds.

Always inside a <PropertyGroup> tag:

<PropertyGroup>
    <BuildDir>Build</BuildDir>
</PropertyGroup>

Conditionals

<SomeProperty Condition="condition">DefaultValue</SomeProperty>

Items

Inputs into the build system; usually files.

  • Grouped into item types based on user-defined item names.
  • Item types are used in tasks which use the individual items to perform the steps of the build process.

An item type named Compile that includes two files:

<ItemGroup>
    <Compile Include = "file1.cs"/>
    <Compile Include = "file2.cs"/>
</ItemGroup>

Referencing Item Types

Use @(ItemType)

Tasks

Executable code that MSBuild projects use to perform build operations.

  • Can be re-used
  • Can be shared across projects

Tasks are children of a Target element. They accept parameters which are passed as attributes to the element:

<Target Name="SomeTask">
    <TaskAction TaskParameter="value" />
</Target>

Properties and Items can both be used as parameters to Tasks.

Common (Built-in) Tasks

Several tasks, like MakeDir, are built in.

<Target Name="MakeBuildDirectory">
    <MakeDir Directories="$(BuildDir)" />
</Target>

Targets

Targets group tasks together in a sequence and expose sections of the project file as entry points into the build process.

Logging for MSBuild

Accessing MSBuild properties with dotnet publish

ℹ️ Important

Availability: .NET 8

dotnet publish 
    --getProperty:<PropertyName>
    --getItem:<ItemType>
    --getTargetResult:<TargetName>