Overwrite properties with MSBuild

前端 未结 4 2063
-上瘾入骨i
-上瘾入骨i 2020-12-10 02:24

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

相关标签:
4条回答
  • 2020-12-10 02:55

    Besides, you can use following way:

    <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-Sub" DependsOnTargets="SetDeployPath-TEST">
      <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>
    
    0 讨论(0)
  • 2020-12-10 03:13

    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>
    
    0 讨论(0)
  • 2020-12-10 03:16

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

    0 讨论(0)
  • 2020-12-10 03:17

    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>
    
    0 讨论(0)
提交回复
热议问题