如何根据msbuild中的条件更改属性的值? [英] How do I change a property's value based on a conditional in msbuild?

查看:89
本文介绍了如何根据msbuild中的条件更改属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果属性值是某个值,我想更改它的值.在C#中,我会写:

I would like to change the value of a property if it is a certain value. In C# I would write:

if(x=="NotAllowed")
  x="CorrectedValue;

这是我到目前为止所拥有的,请不要笑:

This is what I have so far, please don't laugh:

 <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>

推荐答案

您可以在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.

这篇关于如何根据msbuild中的条件更改属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆