Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a PropertyGroup entry from an ItemGroup in MSBuild?

Tags:

msbuild

I'm very new to MSBuild and am having trouble figuring out how to construct a PropertyGroup entry from conditional parts.

Here's what I have, which is not working:

<ItemGroup>
    <CompilerDirective Include="DEBUG_PARANOID" Condition=" '$(SomeFlag)' == 'true' "/>
    <CompilerDirective Include="DEBUG"/>
    <CompilerDirective Include="TRACE"/>
</ItemGroup>

<PropertyGroup>
    ...
    <DefineConstants>@(CompilerDirective)</DefineConstants>
    ...
</PropertyGroup>

I'd like the constants that get defined to show up as DEBUG_PARANOID;DEBUG;TRACE if SomeFlag is set true, leaving out DEBUG_PARANOID if not. This is for a .csproj, by the way.

If I print out @(CompilerDirective) with a message task, it works.

My question is how to make this work inside of a PropertyGroup entry?

like image 445
scobi Avatar asked Mar 17 '11 22:03

scobi


1 Answers

What you have above works. I ran this:

<Target Name="Test">
  <ItemGroup>
      <CompilerDirective Include="DEBUG_PARANOID"
        Condition=" '$(SomeFlag)' == 'true' "/>
      <CompilerDirective Include="DEBUG"/>
      <CompilerDirective Include="TRACE"/>
  </ItemGroup>
  <PropertyGroup>
    <DefineConstants>@(CompilerDirective)</DefineConstants>
  </PropertyGroup>
  <Message Text="$(DefineConstants)" />
</Target>

and got the proper output DEBUG;TRACE or DEBUG_PARANOID;DEBUG;TRACE depending on the value of the property. In what manner does this not work for you?

like image 133
Brian Kretzler Avatar answered Oct 04 '22 04:10

Brian Kretzler