Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge constants of command line and constants of configuration

Tags:

msbuild

I have a project with a preprocessor constant defined for a configuration

<DefineConstants>TRACE;DEBUG;VAR1</DefineConstants>

My code could be :

#if VAR1
        Console.WriteLine("Var1");
#endif
#if VAR2
        Console.WriteLine("Var2");
#endif

I want execute MSBuild and define the constant VAR2 without override constants defined in the configuration

I try :

MSBuild MyProject.csproj /p:DefineConstants=VAR2

But constants defined in the project file are not set : my program displays only

Var2

I have tried things like without success :

MSBuild MyProject.csproj /p:DefineConstants=$(DefineConstants);VAR2

or

<DefineConstants>$(DefineConstants);TRACE;DEBUG;VAR1</DefineConstants>

Is it a way to merge the constants defined in the project and constants defined on the command line?

like image 439
Troopers Avatar asked Sep 14 '25 11:09

Troopers


1 Answers

AFAIK a property defined via commandline will always override a statically defined property with the same name. As a workaround you can enqueue an additional property to <DefineConstants /> that is not defined in your project file:

<DefineConstants>TRACE;DEBUG;VAR1;$(MyCommandLineConstants)</DefineConstants>

and call MSBuild this way:

MSBuild MyProject.csproj /p:MyCommandLineConstants=VAR2;VAR3
like image 63
mkko Avatar answered Sep 17 '25 19:09

mkko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!