tags:

views:

445

answers:

3

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.

+2  A: 

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>
Mehmet Aras
This doesn't seem to work for me. I can use the value of the DeployPath property immediately after the CreateProperty block, but it looses it's value within the Deploy-Sub target.
mdresser
Haa, just found this article http://weblogs.asp.net/bhouse/archive/2006/03/20/440648.aspx describing a bug/feature with the CreateProperty task.
mdresser
Interesting I did not know about this bug. I used CreateProperty before but apparently nothing needed to access the overwritten property outside of the target that overwrote it. Thanks.
Mehmet Aras
No problem, thanks for your help.
mdresser
A: 

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>
mdresser
A: 

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

Sayed Ibrahim Hashimi