5

From powershell

Set-Alias msbuild C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe

and running from the same directory as my sln file

> msbuild /nologo /property:DefineConstants=FOO

works just fine. Let's say I need two build time constants though.

> msbuild /nologo /property:DefineConstants=FOO;BAR

Powershell interprets the semi-colon as an end-of-statement and runs with a single constant and then tries to run the command BAR and errors. So not what I want.

How about?

> msbuild /nologo /property:DefineConstants="FOO;BAR"
MSBUILD : error MSB1006: Property is not valid.
Switch: BAR

This doesn't work either. Not sure what's going on here but it seems to be interpreting "FOO;BAR" as some sort of single thing - not two constants.

So. How do I actually pass in multiple constants?

Edit Upon further investigation, I'm running the following in cmd.exe

>C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /nologo /property:DefineConstants=FOO;BAR
MSBUILD : error MSB1006: Property is not valid.
Switch: BAR

although this works

>C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /nologo /property:DefineConstants="FOO;BAR"
Skarllot
  • 745
  • 6
  • 16
George Mauer
  • 117,483
  • 131
  • 382
  • 612

3 Answers3

8

We can use msbuild escape characters as msbuild /t:rebuild "/property:DefineConstants=PROD%3BCODE_ANALYSIS". Where %3B is to use ; in properties.

https://msdn.microsoft.com/en-us/library/bb383819.aspx

https://msdn.microsoft.com/en-us/library/ms228186.aspx

srinivas n
  • 91
  • 1
  • 3
7

Two ideas:

Add quotes around the property flag and escape the double quotes

msbuild /nologo "/property:DefineConstants=`"FOO;BAR`""

Use the call operator & (works in PS v4+)

& msbuild /nologo /property:DefineConstants="FOO;BAR"
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • Should be noted that while this works for the `DefineConstants` property it breaks for stuff that takes paths if the path contains a space. So in that case do not use this trick. – George Mauer Jul 18 '14 at 18:43
  • @GeorgeMauer String escaping for command line tools is one of the areas where powershell can be difficult to use. I noticed that the second option was working for me on version 4.0 but not version 3.0. – Mike Zboray Jul 18 '14 at 19:27
  • @GeorgeMauer Can you give an example for the case with a path with a space? Are you talking about setting a property to a path with a space or just a path to a proj/sln file? – Mike Zboray Jul 18 '14 at 19:29
  • In my build script I just tried escaping all `/property:...` as you suggested, and that didn't work since some of them were for things like `OutDir`. Only escaping the `DefineConstants` clause worked fine – George Mauer Jul 18 '14 at 19:31
  • You could try using single quotes on the paths, something like `msbuild "/OutDir:'\path\to the\output'"`. – Mike Zboray Jul 18 '14 at 19:34
  • Actually you can just quote the whole property so eg `msbuild /nologo "/property:OutDir=c:\My Secret Apps\bin" "/property:DefineConstants=\`"FOO;BAR\`"` – George Mauer Jul 18 '14 at 19:43
  • Constants separated by simple space character may be used as well: `"/p:DefineConstants=FOO BAR"` – jwaliszko Jan 07 '16 at 01:57
1

None of the solutions above worked for me so I had to employ another approach: https://stackoverflow.com/a/32326853/544947

knocte
  • 16,941
  • 11
  • 79
  • 125