nichos
nichos

Reputation: 157

How to pass dynamic properties to an MSbuild task?

Is it possible to pass a dynamic property group to an MSbuild task? So if I have the following properties and values:

<MyPropertyGroup>
   <Foo>bar</Foo>
   <Foo1>bar1</Foo1>
   <Foo2>bar2</Foo2>
</MyPropertyGroup>

I could call MSBuild with a short property list:

<MSBuild Projects="$(SolutionFile)" Targets="Build" Properties="MyPropertyGroup" />

But it would be the same as calling the task like this:

<MSBuild Projects="$(SolutionFile)" Targets="Build" Properties="Foo=bar;Foo1=bar1;Foo2=bar2" />

This is helpful if there's a large property list, and allows for only 1 place needs to be maintained.

Upvotes: 1

Views: 1821

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100543

There's two issues here:

  • Properties are not grouped, any <PropertyGroup> element is only used to define properties. After evaluation, all properties are just a list of key-value pairs without any grouping, so even a <PropertyGroup Label="my groups"> has no effect on the properties it contains.
  • Items can be used to add more metadata values to the same "key" (item identity). However, iterating over custom metadata can only be done in msbuild tasks (=> code) and not directly within MSBuild, requiring custom tasks and build logic (see this question).

The most practical solution to your problem is to define a single property that contains all the values:

<PropertyGroup>
  <BuildParameters>
    Configuration=Debug;
    Platform=Any CPU;
    SomeOtherProperty=Foo
  </BuildParameters>
</PropertyGroup>
…
<PropertyGroup>
  <!-- this property can even be extended afterwards, e.g. when a condition is needed -->
  <BuildParameters Condition=" '$(ShallAppendThings)' == 'true' ">
    $(BuildParameters);
    AnotherProperty=SomeValue
  </BuildParameters>
</PropertyGroup>
…

<MSBuild Projects="$(SolutionFile)" Targets="Build" Properties="$(BuildProperties)" />

Upvotes: 3

Related Questions