11

It's possible to create properties using either of these methods:

<Target Name="A">
    <PropertyGroup>
        <DogSound>Bark</DogSound>
    </PropertyGroup>
</Target>

<Target Name="B">
    <CreateProperty Value="Bark">
        <Output TaskParameter="Value" PropertyName="DogSound"/>
    </CreateProperty>
</Target>

But, what's the difference between the semantics of targets A and B, if anything?

Thanks.


Note: I'm using msbuild 3.5. I believe the PropertyGroup syntax didn't work inside a target in earlier versions of msbuild, but that was also the same with ItemGroups. CreateItem got deprecated, but CreateProperty didn't, so I'm wondering if CreateProperty still has something over using PropertyGroup, and if so, what.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • 3
    One issue I just ran into with using inside targets is that they are not supported in Mono: http://www.mono-project.com/Microsoft.Build Apparently CreateProperty works fine... –  Jan 09 '13 at 16:45

2 Answers2

9

Don't use CreateProperty & CreateItem in MSBuild 4.0. Instead just place ItemGroup and PropertyGroup directly inside of the target.

You are correct before MSBuild 3.5 ItemGroup/PropertyGroup were not allowed inside of targets so there was CreateProperty & CreateItem tasks that people would use. After MSBuild 3.5 you should just use the ItemGroup & PropertyGroup. Although there are some extreme corner cases where you still might need CreateProperty & CreateItem, but I wouldn't worry about those. These scenarios deal with escaping and how CreateItem is less restrictive then ItemGroup. But in reality 99% of people will not face this.

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
  • Thanks for your answer, I will try and stick to PropertyGroup. However, I was really asking about the 1% of difference people will face. What are the corner cases where PropertyGroup won't do and CreateProperty is needed? – Scott Langham Oct 25 '10 at 08:27
  • @ScottLangham an example would be creating properties that are accessible from other targets ([see this post](http://stackoverflow.com/questions/1366840/overwrite-properties-with-msbuild)) – Joseph Yaduvanshi Mar 12 '12 at 14:32
  • 2
    The only time I would use `CreateItem` or `CreateProperty` is if the new name is only known at runtime. – Eris Jan 29 '16 at 22:22
  • @JimSchubert - I don't think that is quite right. Please see my answer for details. :) – weir Jan 19 '18 at 17:43
  • @weir my comment was in relation to the original question (for msbuild 3.5). Functionality is different now, as it's 5 years later. – Joseph Yaduvanshi Jan 20 '18 at 05:15
3

There is no difference between the behavior of those two targets. That will even remain the case if you add a CallTarget task at the end of both: $(DogSound) will not evaluate to "Bark" in the called target!

However, there will be a difference if you make either of the following changes to target B. Neither is possible using PropertyGroup.

  • Add Input and Output attributes to the Target element and change TaskParameter="Value" to TaskParameter="ValueSetByTask". The latter change would prevent DogSound from being set to "Bark" when target B is skipped due to its outputs being up-to-date with respect to its inputs.
  • Change "DogSound" (the property name) to a dynamic value.

(Even though CreateItem versus ItemGroup is not part of the question, I will address it because the answer is simple. Unlike CreateProperty, CreateItem has been deprecated, so using an in-target ItemGroup is the only choice.)

weir
  • 4,521
  • 2
  • 29
  • 42
  • There's good information here, but you're lacking an actual code example. – keeehlan May 30 '18 at 21:44
  • 1
    @roach-lord - The question was “what's the difference between the semantics of targets A and B, if anything?” – weir May 31 '18 at 01:27
  • The fact that `` DOES set the parameters even when the target gets skipped due to outputs being newer than inputs is rather non-intuitive and may waste you a good deal of time to figure out (binary logging and the binary log viewer can be very helpful). So this answer is very important. – Ivan Kolev Oct 25 '22 at 19:57