Sascha
Sascha

Reputation: 10347

Set property inside custom target

I have a solution I want to build on a CI server containing projects with a custom target like this:

<Target Name="CustomTarget">
  <PropertyGroup>
    <PackageOutputDir>C:\Repos\$(Configuration)</PackageOutputDir>
  </PropertyGroup>
</Target>

Unfortunately the <PackageOutputDir> is specified in different locations for some projects so I want to set it twice.

On the CI server I want to set this to another directory using /property: from msbuild binary like this:

msbuild my.sln /property:PackageOutputDir=$buildPath\ci-output;CustomTarget.PackageOutputDir=$buildPath\ci-output' does not set the value insideCustomTarget`. As I do not control the source, I have to pass the values using the commandline.

Upvotes: 1

Views: 1116

Answers (2)

weir
weir

Reputation: 4761

Change

msbuild my.sln /property:PackageOutputDir=$buildPath\ci-output;CustomTarget.PackageOutputDir=$buildPath\ci-output

to

msbuild my.sln /property:PackageOutputDir=$buildPath\ci-output;PackageOutputDir=$buildPath\ci-output

There is essentially no concept of scope when it comes to MSBuild properties, so the CustomTarget. qualifier is not necessary and sabotages the intended property value assignment.

Upvotes: 0

Michael Baker
Michael Baker

Reputation: 3434

Can you move your PropertyGroup definition outside of the target? If you do this then the command line provided value will overwrite the value in the definition within your target.

For example

<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   
    <PropertyGroup>
      <PackageOutputDir>C:\Repos\$(Configuration)</PackageOutputDir>
    </PropertyGroup>

    <Target Name="CustomTarget"> 
        <Message Text="PackageOutputDir is: $(PackageOutputDir)" />
    </Target>
</Project>  

With the args

msbuild go.build /p:PackageOutputDir="Hello World"

Will produce "Hello World"

Alternatively, and good practice in MSBuild is to always define your properties as conditional.

The following example will also produce "Hello World" as the property will not be evaulated within the target as it already has a value from the command line.

<Target Name="CustomTarget">
    <PropertyGroup>
        <PackageOutputDir Condition="'$(PackageOutputDir)' == ''">C:\Repos\$(Configuration)</PackageOutputDir>
    </PropertyGroup>
    <Message Text="PackageOutputDir is: $(PackageOutputDir)" />
</Target>

Upvotes: 1

Related Questions