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

How to append conditional compilation symbols in project properties with MSBuild?(如何使用 MSBuild 在项目属性中附加条件编译符号?)
本文介绍了如何使用 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 在项目属性中附加条件编译符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
Good-practices: How to reuse .csproj and .sln files to create your MSBuild script for CI?(良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 MSBuild 脚本?)
Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 MSBuild 目标)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)