|
2 | 2 | title: When Element (MSBuild) | Microsoft Docs
|
3 | 3 | description: Learn about the MSBuild When element, which specifies a possible block of code for the Choose element to select.
|
4 | 4 | ms.custom: SEO-VS-2020
|
5 |
| -ms.date: 03/13/2017 |
| 5 | +ms.date: 08/10/2022 |
6 | 6 | ms.topic: reference
|
7 | 7 | f1_keywords:
|
8 | 8 | - http://schemas.microsoft.com/developer/msbuild/2003#When
|
9 | 9 | dev_langs:
|
10 | 10 | - VB
|
11 | 11 | - CSharp
|
12 | 12 | - C++
|
13 |
| -- jsharp |
14 | 13 | helpviewer_keywords:
|
15 | 14 | - <When> Element [MSBuild]
|
16 | 15 | - When Element [MSBuild]
|
@@ -77,50 +76,96 @@ Specifies a possible block of code for the `Choose` element to select.
|
77 | 76 |
|
78 | 77 | ## Example
|
79 | 78 |
|
80 |
| - The following project uses the `Choose` element to select which set of property values in the `When` elements to set. If the `Condition` attributes of both `When` elements evaluate to `false`, the property values in the `Otherwise` element are set. |
| 79 | + The following project uses the `Choose` element to select which set of property values in the `When` elements to set. If the `Condition` attributes of both `When` elements evaluate to `false`, the property values in the `Otherwise` element are set. When running the example, try passing in various property settings from the command line, such as `msbuild myproj.proj -p:Configuration=Test;Platform=x86`, and see what the output path looks like. The example supposes the requirement is to set certain properties for debug and release builds, including the output folder based on the bitness of the platform rather than the actual platform name, and also support the 'Test' and 'Retail' configurations, but treat 'Retail' as 'Release'. |
81 | 80 |
|
82 | 81 | ```xml
|
83 |
| -<Project |
84 |
| - xmlns="http://schemas.microsoft.com/developer/msbuild/2003" > |
| 82 | +<Project> |
85 | 83 | <PropertyGroup>
|
86 |
| - <Configuration Condition="'$(Configuration)' == ''">Debug</Configuration> |
87 |
| - <OutputType>Exe</OutputType> |
88 |
| - <RootNamespace>ConsoleApplication1</RootNamespace> |
89 |
| - <AssemblyName>ConsoleApplication1</AssemblyName> |
90 |
| - <WarningLevel>4</WarningLevel> |
| 84 | + <Configuration Condition="$(Configuration) == ''">Debug</Configuration> |
| 85 | + <Platform Condition="$(Platform) == ''">x64</Platform> |
91 | 86 | </PropertyGroup>
|
92 |
| - <Choose> |
93 |
| - <When Condition=" '$(Configuration)'=='debug' "> |
| 87 | + |
| 88 | + <Choose> |
| 89 | + <When Condition="$(Configuration)=='Test'"> |
| 90 | + <PropertyGroup> |
| 91 | + <DebugSymbols>true</DebugSymbols> |
| 92 | + <DebugType>full</DebugType> |
| 93 | + <Optimize>false</Optimize> |
| 94 | + <DefineConstants>DEBUG;TRACE</DefineConstants> |
| 95 | + </PropertyGroup> |
| 96 | + <Choose> |
| 97 | + <When Condition="$(Platform)=='x86' Or $(Platform) == 'ARM32'"> |
| 98 | + <PropertyGroup> |
| 99 | + <OutputPath>.\bin\Test\32-bit\</OutputPath> |
| 100 | + </PropertyGroup> |
| 101 | + </When> |
| 102 | + <When Condition="$(Platform)=='x64' Or $(Platform) == 'ARM64'"> |
| 103 | + <PropertyGroup> |
| 104 | + <OutputPath>.\bin\Test\64-bit\</OutputPath> |
| 105 | + </PropertyGroup> |
| 106 | + </When> |
| 107 | + <!-- For any other platform, use the platform name --> |
| 108 | + <Otherwise> |
| 109 | + <PropertyGroup> |
| 110 | + <OutputPath>.\bin\Test\$(Platform)\</OutputPath> |
| 111 | + </PropertyGroup> |
| 112 | + </Otherwise> |
| 113 | + </Choose> |
| 114 | + </When> |
| 115 | + <When Condition="$(Configuration)=='Retail' Or $(Configuration)=='Release'"> |
| 116 | + <PropertyGroup> |
| 117 | + <DebugSymbols>false</DebugSymbols> |
| 118 | + <Optimize>true</Optimize> |
| 119 | + <DefineConstants>TRACE</DefineConstants> |
| 120 | + </PropertyGroup> |
| 121 | + <Choose> |
| 122 | + <When Condition="$(Platform)=='x86' Or $(Platform) == 'ARM32'"> |
| 123 | + <PropertyGroup> |
| 124 | + <OutputPath>.\bin\Release\32-bit\</OutputPath> |
| 125 | + </PropertyGroup> |
| 126 | + </When> |
| 127 | + <When Condition="$(Platform)=='x64' Or $(Platform) == 'ARM64'"> |
| 128 | + <PropertyGroup> |
| 129 | + <OutputPath>.\bin\Release\64-bit\</OutputPath> |
| 130 | + </PropertyGroup> |
| 131 | + </When> |
| 132 | + <!-- For any other platform, use the platform name --> |
| 133 | + <Otherwise> |
94 | 134 | <PropertyGroup>
|
95 |
| - <DebugSymbols>true</DebugSymbols> |
96 |
| - <DebugType>full</DebugType> |
97 |
| - <Optimize>false</Optimize> |
98 |
| - <OutputPath>.\bin\Debug\</OutputPath> |
99 |
| - <DefineConstants>DEBUG;TRACE</DefineConstants> |
| 135 | + <OutputPath>.\bin\Release\$(Platform)\</OutputPath> |
100 | 136 | </PropertyGroup>
|
101 |
| - <ItemGroup> |
102 |
| - <Compile Include="UnitTesting\*.cs" /> |
103 |
| - <Reference Include="NUnit.dll" /> |
104 |
| - </ItemGroup> |
105 |
| - </When> |
106 |
| - <When Condition=" '$(Configuration)'=='retail' "> |
| 137 | + </Otherwise> |
| 138 | + </Choose> |
| 139 | + </When> |
| 140 | + <!-- For any other configuration, use debug properties--> |
| 141 | + <Otherwise> |
| 142 | + <PropertyGroup> |
| 143 | + <DebugSymbols>true</DebugSymbols> |
| 144 | + <DebugType>full</DebugType> |
| 145 | + <Optimize>false</Optimize> |
| 146 | + <DefineConstants>DEBUG;TRACE</DefineConstants> |
| 147 | + </PropertyGroup> |
| 148 | + <Choose> |
| 149 | + <When Condition="$(Platform)=='x86' Or $(Platform)=='ARM32'"> |
107 | 150 | <PropertyGroup>
|
108 |
| - <DebugSymbols>false</DebugSymbols> |
109 |
| - <Optimize>true</Optimize> |
110 |
| - <OutputPath>.\bin\Release\</OutputPath> |
111 |
| - <DefineConstants>TRACE</DefineConstants> |
| 151 | + <OutputPath>.\bin\$(Configuration)\32-bit\</OutputPath> |
112 | 152 | </PropertyGroup>
|
113 |
| - </When> |
114 |
| - <Otherwise> |
| 153 | + </When> |
| 154 | + <When Condition="$(Platform)=='x64' Or $(Platform)=='ARM64'"> |
115 | 155 | <PropertyGroup>
|
116 |
| - <DebugSymbols>true</DebugSymbols> |
117 |
| - <Optimize>false</Optimize> |
118 |
| - <OutputPath>.\bin\$(Configuration)\</OutputPath> |
119 |
| - <DefineConstants>DEBUG;TRACE</DefineConstants> |
| 156 | + <OutputPath>.\bin\$(Configuration)\64-bit\</OutputPath> |
120 | 157 | </PropertyGroup>
|
121 |
| - </Otherwise> |
122 |
| - </Choose> |
123 |
| - <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
| 158 | + </When> |
| 159 | + </Choose> |
| 160 | + </Otherwise> |
| 161 | + </Choose> |
| 162 | + |
| 163 | + <Target Name="ShowProperties"> |
| 164 | + <Message Text="DebugSymbols: $(DebugSymbols)"/> |
| 165 | + <Message Text="Optimize: $(Optimize)"/> |
| 166 | + <Message Text="DefineConstants: $(DefineConstants)"/> |
| 167 | + <Message Text="OutputPath: $(OutputPath)"/> |
| 168 | + </Target> |
124 | 169 | </Project>
|
125 | 170 | ```
|
126 | 171 |
|
|
0 commit comments