19

Is there a way to use custom variables on the command-line when building with MSBuild.exe as follows:

MSBuild.exe bootstrapper.msbuild <custom_variable1=custom_variable_value1>

custom_variable2=custom_variable_value2>...<custom_variablen=custom_variable_valuen>

The purpose is to know whether I have to launch another executable or not.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sofiane
  • 908
  • 4
  • 19
  • 45

1 Answers1

33

You should start with the basics. The answer is found in the official documentation.

MSBuild calls these properties instead of variables.

In practice:

msbuild bootstrapper.msbuild /p:custom_variable1=custom_variable_value1

And in the MSBuild file you could use it as such:

<Target Name="MyTarget">
  <PropertyGroup>
    <custom_variable1 Condition="'$(custom_variable1)'==''">defaultValue</custom_variable1>
  </PropertyGroup>
  <Exec Condition="'$(custom_variable1)'=='someValue'" .../>
</Target>

This assigns a default value to the property if it doesn't exist or is empty, and only executes the Exec task if the value is equal to someValue.

mgrandi
  • 3,389
  • 1
  • 18
  • 17
stijn
  • 34,664
  • 13
  • 111
  • 163
  • This is the exact answer I was looking for ... `$(SolutionName)` seems to fail while the project is being loaded so that I have to do my check during the "execution" – Fabrice T Sep 03 '20 at 01:21