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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<BuildDir>C:\Source\Repo\MyRepo\Bin</BuildDir> | |
</PropertyGroup> | |
</Project> |
Properties are always contained in a PropertyGroup element, which occurs directly within the Project element. A PropertyGroup element can contain one or more properties.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<BuildDir>C:\Source\Repo\MyRepo\Bin</BuildDir> | |
<Optimize>false</Optimize> | |
</PropertyGroup> | |
</Project> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<BuildDir>C:\Source\Repo\MyRepo\Bin</BuildDir> | |
</PropertyGroup> | |
<PropertyGroup> | |
<Optimize>false</Optimize> | |
</PropertyGroup> | |
</Project> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | |
<PropertyGroup> | |
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |
<ProjectGuid>{974DF0E9-8535-4E6D-96DD-BF7CCF2B2480}</ProjectGuid> | |
<OutputType>Exe</OutputType> | |
<RootNamespace>ConsoleApp02</RootNamespace> | |
<AssemblyName>ConsoleApp02</AssemblyName> | |
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | |
<FileAlignment>512</FileAlignment> | |
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | |
</PropertyGroup> | |
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |
<PlatformTarget>AnyCPU</PlatformTarget> | |
<DebugSymbols>true</DebugSymbols> | |
<DebugType>full</DebugType> | |
<Optimize>false</Optimize> | |
<OutputPath>bin\Debug\</OutputPath> | |
<DefineConstants>DEBUG;TRACE</DefineConstants> | |
<ErrorReport>prompt</ErrorReport> | |
<WarningLevel>4</WarningLevel> | |
</PropertyGroup> |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<BuildDir>C:\Source\Repo\MyRepo\Bin</BuildDir> | |
<Optimize>false</Optimize> | |
</PropertyGroup> | |
<Target Name = "Compile"> | |
<!– Log the file name of the output file directory –> | |
<Message Text="The output file directory is $(BuildDir)"/> | |
</Target> | |
</Project> |
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:
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup> | |
<BuildDir>C:\Source\Repo\MyRepo\Bin</BuildDir> | |
</PropertyGroup> | |
<PropertyGroup> | |
<BuildDir>C:\Source\Repo\MyRepo\Bin\Debug</BuildDir> | |
</PropertyGroup> | |
</Project> |
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.
[…] In previous blog post, we discussed about the concept of properties in MSBuild schema. We also saw few project files samples and about reserved properties. In this post, we are going to expand that knowledge by discussing how to use properties further. […]
LikeLike