如何使用 MSBuild 在项目属性中附加条件编译符号?

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

问题描述

我陷入了这样一种情况,即我有一个 MSBuild 脚本需要读取项目的构建属性中设置的条件编译符号.我的 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> 并传递 Constants 来自您的 MSBuild/NAnt 脚本.

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 在项目属性中附加条件编译符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 Roslyn 进行编译时源代码修改 下一篇:Antlr4 C# 目标和生成文件的输出路径

相关文章

最新文章