Understanding Microsoft Build System – Properties

In one of the previous post, we discussed about the significance of MSBuild and how to download it. We have also seen the very basics of schema that is needed by MSBuild. In this blog post, we are going to expand on the same by discussing Properties.

Concept of Properties

When you build projects, you frequently compile the source code with different build options. For example, for development environment release, you generally create a build with debug configuration with symbols so that the developers can use it to help finding bugs. For production release, you generally create a build with no symbol information. You would also like to also enable optimizations if its possible. 

The property element defines a variable and a value which can be used to direct MSBuild what to do. For example, you can use it to specify the location of a temporary directory or whether to use Debug or Release configuration or what type of processor you would like to compile source code for.

Defining Properties

MSBuild properties are simply key-value pairs. The key for the property is the name that you will use to refer to the property. The value is its value. For example, the following is a valid project file which defines a property named BuildDir:

Properties are always contained in a PropertyGroup element, which occurs directly within the Project element. A PropertyGroup element can contain one or more properties.

You can create as many PropertyGroup elements under the Project tag as you want. MSBuild engine will process them sequentially. The above project file can also be defined as below:

If you take a look at a project created by Visual Studio, you’ll notice that many properties are declared. These properties have values that will be used throughout the build process for that project. Here is a region from a sample project:

You can see that values for the output type, the name of the assembly, and many others are defined in properties.

Referencing Property

The property value can be referenced using its value inside $(PropertyName). For example, the property in the previous example is referenced by using $(BuildDir). Let’s create a sample project file like below and save it as output-file.csproj:

We have added a Message Task to print to the build output. This task is used to send a message to the logger(s) that are listening to the build process. In many cases this means a message is sent to the console executing the buildIf we build above file, we’ll get response like below:

referencing msbuild properties

Changing Property value

Properties are evaluated in the order in which they appear in the project file. So Property values can be changed by redefining the property again later in the project file. The BuildDir property can be given a new value by using below XML:

Reserved Properties

MSBuild reserves some property names to store information about the project file and the MSBuild binaries. These properties are referenced by using the $ notation, just like any other property. For example, $(MSBuildProjectFile) returns the complete file name of the project file, including the file name extension.

You can find the list of reserved properties at this link.

We’ll discuss further on properties in upcoming posts.

One thought on “Understanding Microsoft Build System – Properties

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s