Search code examples
msbuildoverloadingbuild-script

MSBuild property and variable overloading


Let's say I launch a build script, definition named SampleScript , specifying the parameter /p:MyPrefix=Custom. There is the following code in the script:

<PropertyGroup Condition="$(BuildDefinitionName)=='SampleScript'">
    <MyPrefix>Default</MyPrefix>
</PropertyGroup>

As I get, first MyPrefix is set to "Custom", then it's overloaded to "Default" (am I right?). If so, is there any way to make a user-specified parameter more prioritized, so it's not overloaded?


Solution

  • You can only overload a property specified on the command line by dynamically changing the value inside a target...

    <Project ...>
       <PropertyGroup>
          <Value>FromStaticProperty</Value>
       </PropertyGroup>
    
       <Target Name="PropertyPrecedence">
          <Message Text="Value = '$(Value)'" />
          <PropertyGroup>
             <Value>FromDynamicProperty</Value>
          </PropertyGroup>
          <Message Text="Value = '$(Value)'" />
       </Target>
    </Project>
    
    > msbuild /p:Value=FromCommandLine
    

    In the above example, the "FromStaticProperty" will be ignored given the value supplied from the command line, but the "FromDynamicProperty" will be used.

    Excerpted from MSBuild Trickery trick #27