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.
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.
Contains a set of user-defined Property elements. Every Property element used in an MSBuild project must be a child of a PropertyGroup element.
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>
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>
I typically avoid the CallTarget task. Much better is to use target dependencies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With