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

19

如果属性值是特定的值,我想要更改它的值。在C#中,我会这样写:

if(x=="NotAllowed")
  x="CorrectedValue;
这是我目前为止的代码,请不要嘲笑:
 <PropertyGroup>
    <BranchName>BranchNameNotSet</BranchName>
  </PropertyGroup>

///Other targets set BranchName

 <Target Name="CheckPropertiesHaveBeenSet">
    <Error Condition="$(BranchName)==BranchNameNotSet" Text="Something has gone wrong.. branch name not entered"/>
      <When Condition="$(BranchName)==master">
        <PropertyGroup>
          <BranchName>MasterBranch</BranchName>
        </PropertyGroup>
      </When>
  </Target>
2个回答

25

你可以使用 Property 上的 Condition 来实现这一点:

<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的信息:

ChooseWhenOtherwise元素一起使用,提供了从多个可能选择的代码段中选择一个要执行的方法。

Choose元素可以作为、WhenOtherwise元素的子元素使用。

在您的代码示例中,您使用When而没有使用Choose且在目标内部,这是不可能的。


谢谢madgnome,我把它交给你是因为你明确指出了propertygroup应该放在我的目标内部。 - Noel Kennedy

3

如果值等于'NotAllowed',则将BranchName设置为字符串“CorrectedValue”:

<PropertyGroup>
   <BranchName Condition="'$(BranchName)'=='NotAllowed'">CorrectedValue</BranchName>
</PropertyGroup>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接