Skip to content

Commit 8093c67

Browse files
authored
Merge pull request #10289 from ghogen/issue-8309
MSBuild When element: use a more complex example
2 parents 61dd8be + 077626e commit 8093c67

File tree

1 file changed

+81
-36
lines changed

1 file changed

+81
-36
lines changed

docs/msbuild/when-element-msbuild.md

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
title: When Element (MSBuild) | Microsoft Docs
33
description: Learn about the MSBuild When element, which specifies a possible block of code for the Choose element to select.
44
ms.custom: SEO-VS-2020
5-
ms.date: 03/13/2017
5+
ms.date: 08/10/2022
66
ms.topic: reference
77
f1_keywords:
88
- http://schemas.microsoft.com/developer/msbuild/2003#When
99
dev_langs:
1010
- VB
1111
- CSharp
1212
- C++
13-
- jsharp
1413
helpviewer_keywords:
1514
- <When> Element [MSBuild]
1615
- When Element [MSBuild]
@@ -77,50 +76,96 @@ Specifies a possible block of code for the `Choose` element to select.
7776

7877
## Example
7978

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'.
8180

8281
```xml
83-
<Project
84-
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
82+
<Project>
8583
<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>
9186
</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>
94134
<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>
100136
</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'">
107150
<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>
112152
</PropertyGroup>
113-
</When>
114-
<Otherwise>
153+
</When>
154+
<When Condition="$(Platform)=='x64' Or $(Platform)=='ARM64'">
115155
<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>
120157
</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>
124169
</Project>
125170
```
126171

0 commit comments

Comments
 (0)