Skip to content

Commit 9befcbd

Browse files
Merge pull request #14035 from GitHubber17/440808-g
Freshness Edit: Visual Studio - MSBuild
2 parents 6abf8cc + 65a6e87 commit 9befcbd

File tree

1 file changed

+66
-70
lines changed

1 file changed

+66
-70
lines changed
Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Build the same source files with different options
3-
description: Create different MSBuild build configurations so you can build the same source files with different options and see examples.
4-
ms.date: 11/04/2016
2+
title: Build the Same Source Files With Different Options
3+
description: Create multiple MSBuild build configurations for your source files so you can run builds with different options, and review example configurations.
4+
ms.date: 06/26/2025
55
ms.topic: how-to
66
helpviewer_keywords:
77
- source files, building with different options
@@ -12,152 +12,148 @@ author: ghogen
1212
ms.author: ghogen
1313
manager: mijacobs
1414
ms.subservice: msbuild
15+
#customer intent: As a developer, I want to create multiple MSBuild build configurations for my source files, so I can build the same files by using different options.
1516
---
16-
# Build the same source files with different options
1717

18-
When you build projects, you frequently 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.
18+
# Create multiple build configurations for your source files
19+
20+
When you build projects, you frequently 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 for your source files.
1921

2022
## Use properties to control build settings
2123

22-
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](../msbuild/msbuild-properties.md).
24+
The `Property` element defines a variable that has multiple references in a project file. The variable might identify the location of a temporary directory, or set the values for properties used in several configurations, such as Debug and Release builds. For more information about properties, see [MSBuild properties](msbuild-properties.md).
25+
26+
You can use properties to change the configuration of your build without having to change the project file. The `Condition` attribute of the `Property` and `PropertyGroup` elements allows you to change the value of properties.
2327

24-
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 [Conditions](../msbuild/msbuild-conditions.md).
28+
- To define a property that depends on another property, set the `Condition` attribute in a `Property` element:
2529

26-
### To set a group of properties that depends on another property
30+
```xml
31+
<DebugType Condition="'$(Flavor)'=='DEBUG'">full</DebugType>
32+
```
2733

28-
- Use a `Condition` attribute in a `PropertyGroup` element similar to the following:
34+
- To define a _group_ of properties that depends on another property, set the `Condition` attribute in a `PropertyGroup` element:
2935

30-
```xml
31-
<PropertyGroup Condition="'$(Flavor)'=='DEBUG'">
36+
```xml
37+
<PropertyGroup Condition="'$(Flavor)'=='DEBUG'">
3238
<DebugType>full</DebugType>
3339
<Optimize>no</Optimize>
34-
</PropertyGroup>
35-
```
36-
37-
### To define a property that depends on another property
40+
</PropertyGroup>
41+
```
3842

39-
- Use a `Condition` attribute in a `Property` element similar to the following:
40-
41-
```xml
42-
<DebugType Condition="'$(Flavor)'=='DEBUG'">full</DebugType>
43-
```
43+
For more information about MSBuild conditions, see [Conditions](msbuild-conditions.md).
4444

4545
## Specify properties on the command line
4646

47-
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.
47+
When your project file accepts multiple configurations, you need the ability to change the configurations whenever you build your project. MSBuild supports this action by allowing you to specify properties on the command line with the `-property` or `-p` switch.
4848

49-
### To set a project property at the command line
49+
- To set a project property at the command line, use the `-property` (or `-p`) switch with the property name and value:
5050

51-
- Use the **-property** switch with the property and property value. For example:
51+
```cmd
52+
msbuild file.proj -property:Flavor=Debug
53+
```
5254

53-
```cmd
54-
msbuild file.proj -property:Flavor=Debug
55-
```
55+
- To specify more than one project property at the command line, use the `-property` (`-p`) switch with each property name and value:
5656

57-
or
57+
```cmd
58+
msbuild file.proj -p:Flavor=Debug -p:Platform=x86
59+
```
5860

59-
```cmd
60-
Msbuild file.proj -p:Flavor=Debug
61-
```
61+
- There's a shortcut for specifying multiple properties on the command line. Enter the `-property` (`-p`) switch once, and separate the list of property names and values by using the semicolon (`;`):
6262

63-
### To specify more than one project property at the command line
63+
```cmd
64+
msbuild file.proj -p:Flavor=Debug;Platform=x86;Verbose=True
65+
```
6466

65-
- 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:
67+
## Handle precedence across environment variables and properties
6668

67-
```cmd
68-
msbuild file.proj -p:Flavor=Debug;Platform=x86
69-
```
69+
MSBuild processes environment variable values the same way it handles properties. When the build encounters multiple values for a property, it sets the value according to the order of precedence: command line (highest), project file, and environment variable (lowest).
7070

71-
or
71+
- A property value specified on the command line takes precedence over any value set for the same property in the project file or environment variable.
7272

73-
```cmd
74-
msbuild file.proj -p:Flavor=Debug -p:Platform=x86
75-
```
73+
- A property value set in the project file takes precedence over the corresponding value defined in an environment variable.
7674

77-
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](../msbuild/how-to-use-environment-variables-in-a-build.md).
75+
You can change the precedence behavior by using the `TreatAsLocalProperty` attribute in a project tag. When you list property names with this attribute, the property value specified on the command line doesn't take precedence over the value in the project file. For an example, see [Change precedence with the TreatAsLocalProperty attribute](#change-precedence-with-the-treataslocalproperty-attribute).
7876

79-
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.
77+
For more information, see [Use environment variables in a build](how-to-use-environment-variables-in-a-build.md).
8078

81-
You can change this behavior by using the `TreatAsLocalProperty` attribute in a project tag. For property names that are listed with that attribute, the property value that's specified on the command line doesn't take precedence over the value in the project file. You can find an example later in this topic.
79+
## Use property groups to change build configurations
8280

83-
## Example 1
81+
The following example demonstrates a project file that defines two property groups to build a Debug or Release version of the project.
8482

85-
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.
83+
- To build the Debug version, use the `-property` (`-p`) switch with the `flavor` property value set to `debug`:
8684

87-
To build the debug version of this project, type:
85+
```cmd
86+
msbuild consolehwcs1.proj -p:flavor=debug
87+
```
8888

89-
```cmd
90-
msbuild consolehwcs1.proj -p:flavor=debug
91-
```
89+
- To build the Release version, use the `-property` (`-p`) switch with the `flavor` property value set to `retail`:
9290

93-
To build the retail version of this project, type:
91+
```cmd
92+
msbuild consolehwcs1.proj -p:flavor=retail
93+
```
9494

95-
```cmd
96-
msbuild consolehwcs1.proj -p:flavor=retail
97-
```
95+
Here's the project file:
9896

9997
```xml
10098
<Project DefaultTargets = "Compile"
10199
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
102100

103-
<!-- Sets the default flavor if an environment variable called
104-
Flavor is not set or specified on the command line -->
101+
<!-- Set default flavor, if env variable 'Flavor' not set or specified on command line -->
105102
<PropertyGroup>
106103
<Flavor Condition="'$(Flavor)'==''">DEBUG</Flavor>
107104
</PropertyGroup>
108105

109-
<!-- Define the DEBUG settings -->
106+
<!-- Define DEBUG settings -->
110107
<PropertyGroup Condition="'$(Flavor)'=='DEBUG'">
111108
<DebugType>full</DebugType>
112109
<Optimize>no</Optimize>
113110
</PropertyGroup>
114111

115-
<!-- Define the RETAIL settings -->
112+
<!-- Define RETAIL settings -->
116113
<PropertyGroup Condition="'$(Flavor)'=='RETAIL'">
117114
<DebugType>pdbonly</DebugType>
118115
<Optimize>yes</Optimize>
119116
</PropertyGroup>
120117

121-
<!-- Set the application name as a property -->
118+
<!-- Set application name as a property -->
122119
<PropertyGroup>
123120
<appname>HelloWorldCS</appname>
124121
</PropertyGroup>
125122

126-
<!-- Specify the inputs by type and file name -->
123+
<!-- Specify inputs by type and file name -->
127124
<ItemGroup>
128125
<CSFile Include = "consolehwcs1.cs"/>
129126
</ItemGroup>
130127

131128
<Target Name = "Compile">
132-
<!-- Run the Visual C# compilation using input files
133-
of type CSFile -->
129+
<!-- Run Visual C# compilation using input file of type CSFile -->
134130
<CSC Sources = "@(CSFile)"
135131
DebugType="$(DebugType)"
136132
Optimize="$(Optimize)"
137133
OutputAssembly="$(appname).exe" >
138134

139-
<!-- Set the OutputAssembly attribute of the CSC
140-
task to the name of the executable file that is
141-
created -->
135+
<!-- Set OutputAssembly attribute of CSC task to name of created executable file -->
142136
<Output TaskParameter="OutputAssembly"
143137
ItemName = "EXEFile" />
144138
</CSC>
145-
<!-- Log the file name of the output file -->
139+
<!-- Log file name of output file -->
146140
<Message Text="The output file is @(EXEFile)"/>
147141
</Target>
148142
</Project>
149143
```
150144

151-
## Example 2
145+
## Change precedence with the TreatAsLocalProperty attribute
152146

153-
The following example illustrates how to use the `TreatAsLocalProperty` attribute. The `Color` property has a value of `Blue` in the project file and `Green` in the command line. With `TreatAsLocalProperty="Color"` in the project tag, the command-line property (`Green`) doesn't override the property that's defined in the project file (`Blue`).
147+
The following example illustrates how to use the `TreatAsLocalProperty` attribute. The `Color` property has a value of `Blue` in the project file and `Green` in the command line. With the `TreatAsLocalProperty="Color"` attribute setting in the project tag, the command-line property (`Green`) doesn't override the property value defined in the project file (`Blue`).
154148

155149
To build the project, enter the following command:
156150

157151
```cmd
158152
msbuild colortest.proj -t:go -property:Color=Green
159153
```
160154

155+
Here's the project file:
156+
161157
```xml
162158
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
163159
ToolsVersion="4.0" TreatAsLocalProperty="Color">
@@ -182,7 +178,7 @@ ToolsVersion="4.0" TreatAsLocalProperty="Color">
182178

183179
## Related content
184180

185-
- [MSBuild](../msbuild/msbuild.md)
186-
- [MSBuild concepts](../msbuild/msbuild-concepts.md)
187-
- [MSBuild reference](../msbuild/msbuild-reference.md)
188-
- [Project element (MSBuild)](../msbuild/project-element-msbuild.md)
181+
- [MSBuild](msbuild.md)
182+
- [MSBuild concepts](msbuild-concepts.md)
183+
- [MSBuild reference](msbuild-reference.md)
184+
- [Project element (MSBuild)](project-element-msbuild.md)

0 commit comments

Comments
 (0)