MSBuild 使用 Import 覆盖属性值

MSBuild overwrite properties value with Import

我有一个仅包含 PropertyGroup 的 msbuild 脚本:DefaultVariables.msbuild

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- default values if nothing is set in Main.proj -->
  <PropertyGroup>
    <ProjectName Condition="'$(PublishService)'==''">DefaultService</ProjectName>
  </PropertyGroup>
</Project>

PublishService 可以根据环境进行更改。 我还有一个 Variables.msbuild 与上面的脚本相同,除了服务名称:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- default values if nothing is set in Main.proj -->
  <PropertyGroup>
    <ProjectName Condition="'$(PublishService)'==''">ErpService</ProjectName>
  </PropertyGroup>
</Project

我的主要构建脚本:BuildMsi.msbuild 导入 DefaultVariables.msbuild 并有一个调用 Msi.msbuild

的目标 CreateEnvironmentSpecificInstaller
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CreateInstaller">

  <PropertyGroup>
    <BaseDir Condition="$(BaseDir)==''">$(MSBuildProjectDirectory)</BaseDir>
  </PropertyGroup>

  <Import Project="DefaultVariables.msbuild" />  

  <!-- Something else -->
  <Target Name="CreateEnvironmentSpecificInstaller" DependsOnTargets="$(SpecificBuildSteps)">
    <MSBuild Projects="$(RedistDir)\Framework\Msi.msbuild" Targets="CreateBatchScripts" StopOnFirstFailure="true" Properties="Configuration=$(Configuration)" RebaseOutputs="true" />
  </Target>

  <Target Name="CreateInstaller" DependsOnTargets="PrintVersion;$(GenericBuildSteps)">
    <MSBuild Condition=" '$(EnvironmentName)' == '**AllEnvironments**' " Projects="$(BaseDir)$(BtsDeploymentFrameworkDir)\BuildMsi.msbuild" Targets="CreateEnvironmentSpecificInstaller" StopOnFirstFailure="true"
        Properties="Configuration=$(Configuration);" RebaseOutputs="true" />
    <CallTarget Targets="RemoveGeneratedEnvironmentSettings" />
  </Target>
</Project>

在 Msi.msbuild 脚本中我添加了一个 Import to Variables.msbuild 脚本,但是此后的 PublishService 仍然是 DefaultService:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="CreateInstaller">

  <Import Project="Variables.msbuild" />
  <Target Name="CreateBatchScripts">
    <Message Text="PublishService = $(PublishService)" />
  </Target>
</Project>

如何在运行时覆盖此 属性 值?

首先,你从来没有给PublishService赋值。我假设 DefaultVariables.msbuild 你想做的是

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- default values if nothing is set in Main.proj -->
  <PropertyGroup>
    <PublishService> Condition="'$(PublishService)'==''">DefaultService</PublishService>
  </PropertyGroup>
</Project>

那么,我建议你也把Variables.msbuild中的属性重命名,去掉条件Condition="'$(PublishService)'==''。由于您在 DefaultVariables.msbuild 中为 属性 提供了默认值,因此条件不会满足,因此该值不会更改。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PublishService>ErpService</PublishService>
  </PropertyGroup>
</Project>