如何使用MSBuild在项目属性中附加条件编译符号? [英] How to append conditional compilation symbols in project properties with MSBuild?

查看:154
本文介绍了如何使用MSBuild在项目属性中附加条件编译符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个MSBuild脚本需要读取项目的build属性中设置的条件编译符号时,我陷入了困境.我的MSBuild脚本文件中有以下代码

I am stuck in a situation where I have an MSBuild script that needs to read the conditional compilation symbols set in project's build property. I have the following code in my MSBuild script file

  <PropertyGroup>
    <DefineConstants>$(DefineConstants);INTER</DefineConstants>
  </PropertyGroup>


  <Target Name="Compile">
    <Message Text="$(DefineConstants)"/>
    <MSBuild Projects="CustomAssemblyInfo.csproj" Targets="Rebuild"  Properties="DefineConstants=$(DefineConstants)" />
  </Target>

我假设$(DefineConstants);将包含已设置的条件编译符号的值,我可以在这些值之后附加任何内容,例如本例中的INTER,但是不知何故在此处未传递项目属性中设置的值.有人可以帮我解决我所缺少的吗?

I was assuming that the $(DefineConstants); will contain the value of conditional compilation symbols that are set and I can just append anything after those values like in this case INTER but somehow the values set in the project properties are not getting passed here. Can anyone please help in what am I missing?

推荐答案

通过MSBuild任务的Properties属性传递的属性称为 global 属性,与通过/p:传递的属性相同在命令行上.它们优先于其他任何属性或环境变量,甚至是无条件定义的变量,即.csproj中的DefineConstants.

Properties passed via Properties property of MSBuild task are what's called global properties, same as those passed with /p: on command line. They take priority over any other property or environment variable even those defined unconditionally, i.e. the DefineConstants in your .csproj.

首先通过传递自己的DefineConstants,可以防止以后在.csproj中进行设置,因此要防止它在项目属性窗口中添加类似$(Constants)的内容,这会将DefineConstants重新定义为<DefineConstants>TRACE;DEBUG;$(Constants)</DefineConstants>并通过而是从您的MSBuild/NAnt脚本中删除Constants.

By passing your own DefineConstants first you prevent it being set later from the .csproj, so to prevent it add something like $(Constants) in your project properties window which would redefine DefineConstants as <DefineConstants>TRACE;DEBUG;$(Constants)</DefineConstants> and pass Constants from your MSBuild/NAnt script instead.

根据下面的@ sǝɯɐſ评论

As per @sǝɯɐſ comment below

https://i.imgur.com/jZiVy7J.png

这篇关于如何使用MSBuild在项目属性中附加条件编译符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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