且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何根据msbuild中的条件更改属性的值?

更新时间:2022-04-13 22:32:15

您可以在Property上使用Condition来做到这一点:

You can do that using Condition on Property:

<PropertyGroup>
  <BranchName>BranchNameNotSet</BranchName>
</PropertyGroup>

<Target Name="CheckPropertiesHaveBeenSet">
  <!-- If BranchName equals 'BranchNameNotSet' stop the build with error-->
  <Error Condition="'$(BranchName)'=='BranchNameNotSet'" Text="Something has gone wrong.. branch name not entered"/>

  <PropertyGroup>
    <!-- Change BranchName value if BranchName equals 'master' -->
    <BranchName Condition="'$(BranchName)'=='master'">MasterBranch</BranchName>
  </PropertyGroup>

</Target>

WhenChoose上的信息:

Info on When and Choose:

选择",何时"和否则"元素一起使用,以提供一种从一系列可能的选择中选择要执行的一段代码的方法.

The Choose, When, and Otherwise elements are used together to provide a way to select one section of code to execute out of a number of possible alternatives.

选择元素可以用作Project,When和else元素的子元素.

Choose elements can be used as child elements of Project, When and Otherwise elements.

在代码示例中,您无法在目标中使用When而没有Choose.

In your code sample, you use When without Choose and within a target, that is not possible.