Properties are key/value pairs that can be used to configure builds. Properties are useful for passing values to tasks, evaluating in conditions, and storing values that will be referenced thoughout the project file.
Defining and Referencing Properties in a Project File
Properties are declared by creating an element with the name of the property as a child of a PropertyGroup element. For example, the following XML creates a property named BuildDir with a value of Build.
<PropertyGroup> <BuildDir>Build</BuildDir> </PropertyGroup>
You reference properties throughout the project file with the syntax $(PropertyName). For example, you reference the property in the previous example with $(BuildDir).
Setting Properties from the Command Line
MSBuild allows you to set properties from the command line using the /property or /p command line switch. Property values received from the command line override property values set in the project file and property values inherited from environment variables.
The following example sets the Configuration property to DEBUG.
MSBuild.exe MyProj.proj /p:Configuration=DEBUG
MSBuild reserves some property names to store information about the project file and the MSBuild binaries. These properties are referenced with the $ notation like any other property. For more information, seeand .
You can reference environment variables in project files the same way as reserved properties. For example, to use the PATH environment variable in your project file, use $(Path). If the project contains a property definition that has the same name as an environment variable, the property in the project overrides the value of the environment variable. For more information, see.
Storing XML in Properties
Properties can contain arbitrary XML, which can aid in passing values to tasks or displaying logging information. The following example shows the ConfigTemplate property with a value containing XML and other property references. MSBuild replaces the property references with their respective property values. Property values are interpreted from top to bottom, so in this example, $(MySupportedVersion), $(MyRequiredVersion), and $(MySafeMode) should have already been defined.
<PropertyGroup> <ConfigTemplate> <Configuration> <Startup> <SupportedRuntime ImageVersion="$(MySupportedVersion)" Version="$(MySupportedVersion)"/> <RequiredRuntime ImageVersion="$(MyRequiredVersion) Version="$(MyRequiredVersion)" SafeMode="$(MySafeMode)"/> </Startup> </Configuration> </ConfigTemplate> </PropertyGroup>