This documentation is archived and is not being maintained.

How To: Build the Same Source Files with Different Options

When you build projects, it is often necessary to compile the same components with different build options. For example, you can create a debug build with symbol information or a release build with no symbol information but with optimizations enabled. Or you can build a project to run on a specific platform, such as x86 or x64. In all these cases, most of the build options stay the same; only a few options are changed to control the build configuration. With MSBuild, you use properties and conditions to create the different build configurations.

Using Properties to Modify Projects

The Property element defines a variable that is referenced several times in a project file, such as the location of a temporary directory, or to set the values for properties that are used in several configurations, such as a Debug build and a Release build. For more information about properties, see MSBuild Properties.

You can use properties to change the configuration of your build without having to change the project file. The Condition attribute of the Property element and the PropertyGroup element allows you to change the value of properties. For more information about MSBuild conditions, see MSBuild Conditions.

To set a group of properties based on another property

  • Use a Condtion attribute in a PropertyGroup element similar to the following:

    <PropertyGroup Condition="'$(Flavor)'=='DEBUG'">

To define a property based on another property

  • Use a Condition attribute in a Property element similar to the following:

    <DebugType Condition="'$(Flavor)'=='DEBUG'">full</DebugType>

Specifying Properties on the Command Line

Once your project file is written to accept multiple configurations, you need to have the ability to change those configurations whenever you build your project. MSBuild provides this ability by allowing properties to be specified on the command line using the /property or /p switch.

To set a project property at the command line

  • Use the /property switch with the property and property value. For example:

    msbuild file.proj /property:Flavor=Debug

    - or -

    Msbuild file.proj /p:Flavor=Debug

To specify more than one project property at the command line

  • Use the /property or /p switch multiple times with the property and property values, or use one /property or /p switch and separate multiple properties with semicolons (;). For example:

    msbuild file.proj /p:Flavor=Debug;Platform=x86

    - or-

    msbuild file.proj /p:Flavor=Debug /p:Platform=x86

Environment variables are also treated as properties and are automatically incorporated by MSBuild. For more information about using environment variables, see How To: Use Environment Variables in a Build.


The property value that is specified on the command line takes precedence over any value that is set for the same property in the project file, and that value in the project file takes precedence over the value in an environment variable.


The following code example, the "Hello World" project contains two new property groups that can be used to create a Debug build and a Release build.

To build the debug version of this project, type:

msbuild consolehwcs1.proj /p:flavor=debug

To build the retail version of this project, type:

msbuild consolehwcs1.proj /p:flavor=retail

<Project DefaultTargets = "Compile"

    <!-- Sets the default flavor of an environment variable called 
    Flavor is not set or specified on the command line -->
        <Flavor Condition="'$(Flavor)'==''">DEBUG</Flavor>

    <!-- Define the DEBUG settings -->
    <PropertyGroup Condition="'$(Flavor)'=='DEBUG'">

    <!-- Define the RETAIL settings -->
    <PropertyGroup Condition="'$(Flavor)'=='RETAIL'">

    <!-- Set the application name as a property -->

    <!-- Specify the inputs by type and file name -->
        <CSFile Include = "consolehwcs1.cs"/>

    <Target Name = "Compile">
        <!-- Run the Visual C# compilation using input files
        of type CSFile -->
        <CSC  Sources = "@(CSFile)"
            OutputAssembly="$(appname).exe" >

            <!-- Set the OutputAssembly attribute of the CSC
            task to the name of the executable file that is 
            created -->
            <Output TaskParameter="OutputAssembly"
                ItemName = "EXEFile" />
        <!-- Log the file name of the output file -->
        <Message Text="The output file is @(EXEFile)"/>

See Also