使用 Team City 环境变量覆盖项目属性
我有一个名为 Version 的 C# 项目属性,定义为
<Version Condition="$(Version)==''">1.2.3.4<Version>
1.2.3.4 是默认值。
我有一个 Team City 系统属性,也称为版本,设置为覆盖。因此,在 Team City 的自定义运行对话框中,我可以为版本指定一个值,然后使用该值。这很好用。
但是,如果我在 Team City 中将参数留空,默认值仍会被空白覆盖(空?)。如果我删除 Team City 参数,则使用默认值。
条件不正确吗?如何将 Team City 属性设置为空白,并且仅在输入某个值时才覆盖?
I have a C# project property called Version defined as
<Version Condition="$(Version)==''">1.2.3.4<Version>
1.2.3.4 is the default value.
I have a Team City system property, also called Version, set up to override. So in the custom run dialog in Team City, I can specify a value for Version and that value gets used. This works fine.
If I leave the parameter blank in Team City, however, the default value is still overwritten with blank (null?). If I delete the Team City parameter, the default value is used.
Is the condition incorrect? How can I set up the Team City property to be blank, and only override if I enter some value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OP 评论后更新了答案:
来自文档:
因此,您只需在 TeamCity 配置中设置属性 $(VersionTC) 并检查该属性是否为空并设置版本
(因此您首先将
Version
设置为VersionTC
。然后看看它是否为空并设置默认值)看看这个 博客文章解释了这一切。
尝试如下所示:
注意
$(Version)
周围的' '
(单引号)Updated answer after OP's comment:
From docs:
So you can just set a property $(VersionTC) in TeamCity configuration and check if that property is empty or not and set version
( so you set
Version
toVersionTC
first. Then see if it empty and set the default )Have a look at this blog post explaining all this.
Try something like below:
Note the
' '
(single quotes) around$(Version)
Team City 可能仍在命令行上传递参数,只是使用空白值,如
或类似的值。您看到的症状是由于 MSBuild 处理重写属性的方式造成的。当在命令行上指定时,属性将采用该值,无论它是否也在静态(文件中全局,而不是目标内部)PropertyGroup 声明中声明。因此,您的版本声明以及检查空字符串的条件将被完全跳过。
解决此问题的一种方法是将包含 $(Version) 声明及其条件的 PropertyGroup 移至首次使用的目标内。 MSBuild 将允许从目标内运行时创建的“动态”属性覆盖命令行属性的值。
如果您运行此命令行...
...并具有此目标...
...您将获得显示 1.2.3.4 的版本,而在目标外部的 PropertyGroup 中,它将保留空值。
Team City is probably still passing the parameter on the command line, just with a blank value, as in,
or something similar. The symptom you are seeing is due to how MSBuild deals with overridden properties. When specified on a command line, a property will take that value whether or not it is also declared in a static (global in the file, not inside a target) PropertyGroup declaration. So your declaration of Version, with the Condition being checked for teh empty string, is being skipped entirely.
One way around this is to move your PropertyGroup containing the declaration of $(Version), with its Condition, inside the target where it is first used. MSBuild will allow overwriting the value of a command line property from a "dynamic" property created at runtime from within a target.
If you run this command line...
...and have this target...
... you will get Version showing 1.2.3.4, whereas with the PropertyGroup outside the target, it will retain the empty value.