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