Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MsBuild, what's the difference between CreateProperty vs PropertyGroup?

Tags:

msbuild

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.

like image 523
Scott Langham Avatar asked Oct 22 '10 16:10

Scott Langham


2 Answers

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.)

like image 35
weir Avatar answered Oct 09 '22 13:10

weir


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.

like image 102
Sayed Ibrahim Hashimi Avatar answered Oct 09 '22 14:10

Sayed Ibrahim Hashimi