14

I am stuck in a situation where I have an MSBuild script that needs to read the conditional compilation symbols set in project's build property. I have the following code in my MSBuild script file

  <PropertyGroup>
    <DefineConstants>$(DefineConstants);INTER</DefineConstants>
  </PropertyGroup>


  <Target Name="Compile">
    <Message Text="$(DefineConstants)"/>
    <MSBuild Projects="CustomAssemblyInfo.csproj" Targets="Rebuild"  Properties="DefineConstants=$(DefineConstants)" />
  </Target>

I was assuming that the $(DefineConstants); will contain the value of conditional compilation symbols that are set and I can just append anything after those values like in this case INTER but somehow the values set in the project properties are not getting passed here. Can anyone please help in what am I missing?

Afraz Ali
  • 2,442
  • 2
  • 22
  • 39

1 Answers1

23

Properties passed via Properties property of MSBuild task are what's called global properties, same as those passed with /p: on command line. They take priority over any other property or environment variable even those defined unconditionally, i.e. the DefineConstants in your .csproj.

By passing your own DefineConstants first you prevent it being set later from the .csproj, so to prevent it add something like $(Constants) in your project properties window which would redefine DefineConstants as <DefineConstants>TRACE;DEBUG;$(Constants)</DefineConstants> and pass Constants from your MSBuild/NAnt script instead.

Edit: As per @sǝɯɐſ comment below

https://i.imgur.com/jZiVy7J.png

enter image description here

Ilya Kozhevnikov
  • 9,626
  • 4
  • 34
  • 66
  • Thanks llya, but I am wondering why can't I simply read the values passed in from the project properties in msbuild files? – Afraz Ali Jun 12 '14 at 02:24
  • In my case I just want to read what ever is passed in and then just append some more constants in msbuild. Also please note that I have a separate XML file for MSBuild, I am not using changing the csproj itself. – Afraz Ali Jun 12 '14 at 02:26
  • @AfrazAli you can't read it cause you're going from custom script to project file and not other way round, there are simply not loaded and evaluated yet. You can try reading and parsing it yourself as text, as XML or with Microsoft.Build.Evaluation, otherwise you need to go to project properties and add a $(Placeholder) in 'conditional compilation symbols' under Build tab that you can then control from your MSBuild, NAnt or .bat script. – Ilya Kozhevnikov Jun 12 '14 at 09:08
  • Ok, Now I understand, Thanks for clarifying the concept! – Afraz Ali Jun 12 '14 at 10:17
  • Can you please show me a code snippet, how to declare a property in MS Build file and then pass it in project properties constants? – Afraz Ali Jun 13 '14 at 07:22
  • Thanks a lot, Helped big time! – Afraz Ali Jun 13 '14 at 09:18
  • 1
    You should add that screenshot to your answer! Helped me as well, thanks! – sǝɯɐſ Apr 06 '15 at 17:53