Search code examples
msbuildmsbuild-task

MSBuild overwrite properties value with Import


I have a msbuild script which contains only PropertyGroup: 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>

The PublishService can be changed depend on environment. I also have a Variables.msbuild with is same with above script, except the service name:

<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

My main build script: BuildMsi.msbuild import the DefaultVariables.msbuild and has a target CreateEnvironmentSpecificInstaller which calls to Msi.msbuild

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

In Msi.msbuild script I add an Import to Variables.msbuild script, but the PublishService after this is still the 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>

How can I overwrite this property value in runtime?


Solution

  • First of all, you are never giving a value to PublishService. I assume in DefaultVariables.msbuild what you wanted to do is

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

    Then, I suggest you also rename the property in Variables.msbuild and remove the condition Condition="'$(PublishService)'==''. As you have given the property a default value in DefaultVariables.msbuild, the condition will not be fulfilled and thus the value will not be changed.

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