开发者

Using a Team City environment variable to override a project property

开发者 https://www.devze.com 2023-03-08 14:45 出处:网络
I have a C# project property called Version defined as <Version Condition=\"$(Version)==\'\'\">1.2.3.4<Version>

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 开发者_StackOverflowup the Team City property to be blank, and only override if I enter some value?


Updated answer after OP's comment:

From docs:

MSBuild allows you to set properties from the command line using the /property or /p command line switch. Property values received from the command line override property values set in the project file and property values inherited from environment variables.

So you can just set a property $(VersionTC) in TeamCity configuration and check if that property is empty or not and set version

<Version>$(VersionTC)<Version>
<Version Condition="'$(VersionTC)'==''">1.2.3.4<Version>

( so you set Version to VersionTC first. Then see if it empty and set the default )

Have a look at this blog post explaining all this.


Try something like below:

<Version Condition=" '$(Version)'=='' ">1.2.3.4<Version>

Note the ' ' (single quotes) around $(Version)


Team City is probably still passing the parameter on the command line, just with a blank value, as in,

/p:Version=""

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

> msbuild My.proj /t:Ver /p:Version=""

...and have this target...

<Target Name="Ver">
  <PropertyGroup>
    <Version Condition="'$(Version)' == ''">1.2.3.4<Version>
  </PropertyGroup>
  <Message Text="Version: '$(Version)'" />
</Target>

... you will get Version showing 1.2.3.4, whereas with the PropertyGroup outside the target, it will retain the empty value.

0

精彩评论

暂无评论...
验证码 换一张
取 消