Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite properties with MSBuild

Tags:

msbuild

I want to declare three properties in my MSBuild file and overwrite one property with the value of another (depending on the target being called), but can't figure out how to do this. My build file looks something like this:

<PropertyGroup>
   <DeployPath_TEST>\\test-server-path\websites\mysite</DeployPath_TEST>
   <DeployPath_LIVE>\\live-server-path\websites\mysite</DeployPath_LIVE>
   <DeployPath></DeployPath>
</PropertyGroup>

<Target Name="Deploy-TEST">
   <PropertyGroup>
      <DeployPath>$(DeployPath_TEST)</DeployPath>
   </PropertyGroup>
   <CallTarget Targets="Deploy-Sub"/>
</Target>

<Target Name="Deploy-LIVE">
   <PropertyGroup>
      <DeployPath>$(DeployPath_TEST)</DeployPath>
   </PropertyGroup>
   <CallTarget Targets="Deploy-Sub"/>
</Target>

<Target Name="Deploy-Sub">
   <Message Text="Deploying to $(DeployPath)"/>
   <MSBuild Projects="MySolution.csproj" Targets="Rebuild" />

   <ItemGroup>
     <MyFiles Include="**\*"/>
   </ItemGroup>

   <Copy SourceFiles="@(MyFiles)" 
         DestinationFiles="@(MyFiles->'$(DeploymentPath)\%(RecursiveDir)%(FileName)%(Extension)')"/>

</Target>

At the moment I'm trying to re-declare the property setting it's value accordingly, but this isn't working.

like image 687
Matthew Dresser Avatar asked Sep 02 '09 10:09

Matthew Dresser


People also ask

How do I change properties in MSBuild?

MSBuild lets you set properties on the command line by using the -property (or -p) switch. These global property values override property values that are set in the project file. This includes environment properties, but does not include reserved properties, which cannot be changed.

What is Property Group in MSBuild?

Contains a set of user-defined Property elements. Every Property element used in an MSBuild project must be a child of a PropertyGroup element.


3 Answers

Mehmet is right about how to set a property value from another property, but there is a bug/feature in MSBuild which means that if you call CreateProperty and CallTarget in the same Target, your new property will not be globally available to other targets (described here).

So here is the final solution to the problem:

<PropertyGroup>
   <DeployPath_TEST>\\test-server-path\websites\mysite</DeployPath_TEST>
   <DeployPath_LIVE>\\live-server-path\websites\mysite</DeployPath_LIVE>
   <DeployPath></DeployPath>
</PropertyGroup>

<Target Name="SetDeployPath-TEST">
  <CreateProperty Value="$(DeployPath_TEST)">
    <Output TaskParameter="Value" PropertyName="DeployPath"/>
  </CreateProperty>
</Target>

<Target Name="Deploy-TEST">
   <CallTarget Targets="SetDeployPath-TEST"/>
   <CallTarget Targets="Deploy-Sub"/>
</Target>

<Target Name="Deploy-Sub">
  <Message Text="Deploying to $(DeployPath)"/>
  <MSBuild Projects="MySolution.csproj" Targets="Rebuild" />

  <ItemGroup>
    <MyFiles Include="**\*"/>
  </ItemGroup>

  <Copy SourceFiles="@(MyFiles)" 
     DestinationFiles="@(MyFiles->'$(DeploymentPath)\%(RecursiveDir)%(FileName)%(Extension)')"/>

</Target>
like image 117
Matthew Dresser Avatar answered Oct 21 '22 06:10

Matthew Dresser


You can use CreateProperty task to overwrite the value of an existing property.

<Target Name="Deploy-LIVE">   
  <CreateProperty Value="$(DeployPath_LIVE)">
    <Output PropertyName="DeployPath" TaskParameter="Value"/>
  </CreateProperty>
  <CallTarget Targets="Deploy-Sub"/>
</Target>
like image 22
Mehmet Aras Avatar answered Oct 21 '22 06:10

Mehmet Aras


I typically avoid the CallTarget task. Much better is to use target dependencies.

like image 4
Sayed Ibrahim Hashimi Avatar answered Oct 21 '22 08:10

Sayed Ibrahim Hashimi