MSBuild 条件构造MSBuild conditional constructs

MSBuild 为非此即彼的处理机制提供 ChooseWhenOtherwise 元素。MSBuild provides a mechanism for either/or processing with the Choose, When, and Otherwise elements.

使用 Choose 元素Use the Choose element

Choose 元素包含一系列具有 Condition 属性的 When 元素,这些元素按照从顶部到底部的顺序进行测试,直到一个元素的计算结果为 trueThe Choose element contains a series of When elements with Condition attributes that are tested in order from top to bottom until one evaluates to true. 如果多个 When 元素的计算结果为 true,则只使用第一个。If more than one When element evaluates to true, only the first one is used. 如果 When 元素的任何条件的计算结果均为 true,则评估 Otherwise 元素(如存在)。An Otherwise element, if present, will be evaluated if no condition on a When element evaluates to true.

Choose 元素可用作 ProjectWhenOtherwise 元素的子元素。Choose elements can be used as child elements of Project, When and Otherwise elements. WhenOtherwise 元素可具有 ItemGroupPropertyGroupChoose 子元素。When and Otherwise elements can have ItemGroup, PropertyGroup, or Choose child elements.

示例Example

以下示例使用 ChooseWhen 元素进行非此即彼的处理。The following example uses the Choose and When elements for either/or processing. 根据 Configuration 属性的值设置项目的属性和项。The properties and items for the project are set depending on the value of the Configuration property.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <OutputType>Exe</OutputType>
        <RootNamespace>ConsoleApplication1</RootNamespace>
        <AssemblyName>ConsoleApplication1</AssemblyName>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <Choose>
        <When Condition=" '$(Configuration)'=='Debug' ">
            <PropertyGroup>
                <DebugSymbols>true</DebugSymbols>
                <DebugType>full</DebugType>
                <Optimize>false</Optimize>
                <OutputPath>.\bin\Debug\</OutputPath>
                <DefineConstants>DEBUG;TRACE</DefineConstants>
            </PropertyGroup>
            <ItemGroup>
                <Compile Include="UnitTesting\*.cs" />
                <Reference Include="NUnit.dll" />
            </ItemGroup>
        </When>
        <When Condition=" '$(Configuration)'=='retail' ">
            <PropertyGroup>
                <DebugSymbols>false</DebugSymbols>
                <Optimize>true</Optimize>
                <OutputPath>.\bin\Release\</OutputPath>
                <DefineConstants>TRACE</DefineConstants>
            </PropertyGroup>
        </When>
    </Choose>
    <!-- Rest of Project -->
</Project>

在此示例中,使用了关于编译器常量 DEFINED_CONSTANT 的条件。In this example, a condition on a compiler constant DEFINED_CONSTANT is used. 这些内容包含在 DefinedConstants 属性中。These are included in the DefinedConstants property. 正则表达式用于匹配分号分隔列表中的确切常量。The regular expression is used to match the exact constant in a semicolon-separated list.

<Choose>
   <When Condition="$([System.Text.RegularExpressions.Regex]::IsMatch(
         $(DefineConstants), '^(.*;)*DEFINED_CONSTANT(;.*)*$'))">
      <!-- When DEFINED_CONSTANT is defined. -->
   </When>
   <!-- other conditions -->
</Choose>

请参阅See also