Learn MSBuild - Part 2 - Variables

MSBuild is a domain-specific language, tailored to customizing how a project is built and compiled. It shares concepts with any general-purpose language, and in this post we’ll explore how MSBuild handles variables.

MSBuild has two variable types: Properties (i.e. strings) and Items (i.e. object arrays).

Properties (i.e. strings)

A string variable is declared by locating an XML node inside PropertyGroup XML node.

    <CoolProperty>wow! this is so cool</CoolProperty>

In many ways, MSBuild behaves similarly to a general-purpose language. Here would be the equivalent C#:

var coolProperty = "wow! this is so cool!";

Properties can be reassigned at any time.

    <Direction>Go left</Direction>
    <Direction>Sorry! Right!</Direction>
var direction = "Go left";
direction = "Sorry! Right!";

Tip: Whitespace matters!

Any and all whitespace inside the XML node is respected.

var isFoo = @"

You can access the value of a property (using $) to define other strings (i.e. string templating).

<Greeting>Hello $(Name)!</Greeting> <!-- Hello Lizzy! -->
var name = "Lizzy";
var greeting = $"Hello {name}!"; // Hello Lizzy!

Properties can also be defined conditionally using the Condition attribute.

<UsesNpm Condition="Exists('package.json')">true</UsesNpm>
var usesNpm = File.Exists(Path.Combine(Environment.CurrentDirectory, "package.json"))
    ? true
    : string.Empty;

Note: Learn more about the available Condition operators in the official documentation

Items (i.e. object arrays)

Am Item is defined by locating an XML node inside ItemGroup XML node.

    <!-- single item -->
    <FavoriteThings Include="Raindrops on roses" />
    <!-- multiple items -->
    <FavoriteThings Include="Whiskers on kittens; bright copper kettles" />
var favoriteThings = new[] { "Raindrops on roses" };
favoriteThings = favoriteThings
    .Append("Whiskers on kittens")
    .Append("bright copper kettles");

You can access the value of a Item (using @).

    <CoolFiles Include="A.txt; B.txt">

<Message Text="@(CoolFiles)" /> <!-- A.txt;B.txt -->
var coolFiles = new[] { "A.txt", "B.txt" };
Console.WriteLine(string.Join(";", coolFiles)); // "A.txt;B.txt"

MSBuild is designed to make working with files very easy. You can use wildcards to filter all the files included in the project.

<NonMinifiedFiles Include="*.js" Remove="*.min.js">
var nonMinifiedFiles = Directory.EnumerateFiles(Environment.CurrentDirectory, "*.js", SearchOption.AllDirectories)
    .Where(f => !f.EndsWith(".min.js"));

Items can be arrays of more complex objects. The child XML nodes of an Item are called “metadata” and can be accessed using the % operator.

    <Stuff Include="Hide.cs" >
    <Stuff Include="Display.cs">
<Message Text="@(Stuff)" Condition=" '%(Display)' == 'true' "/> <!-- Display.cs -->
var stuff = new[] { new { Name = "Hide.cs", Display = false }, new { Name = "Display.cs", Display = true };
Console.WriteLine(string.Join(";", stuff.Where(s => s.Display))); // "Display.cs"


MSBuild has the same primitives as general-purpose languages like C#.

In this post, we covered its support for variables. In the next post, we’ll cover its support for functions!

Appendix: A quick note on variable names

A source of MSBuild consternation is the language’s lack of variable scoping. There are no “private” variables, so you can overwrite variables defined by any MSBuild target, including the “standard library” that facilitates the compilation of your project’s C#!

To avoid collisions, I recommend “namespacing” custom variables:

<!-- Very likely to collide! -->

<!-- Much better! -->

<!-- Even better! -->

This “feature” of language does have perks. It makes exposing a public API for disabling a build feature dead simple:

<!-- From a NuGet package the minifies your code, provide a default that can be overridden -->
<MinifyJavaScriptFiles Condition="'$(GenerateMinifiedSourceMaps)' ==''">true</MinifyJavaScriptFiles>

<!-- In a consuming project, disable minification in local dev for better performance -->
<MinifyJavaScriptFiles Condition="$(IsLocalDev)">false</MinifyJavaScriptFiles>

Tip: Avoid naming collisions with the standard library!

Here is the documentation on built-in Property and Item names: