In MsBuild, what's the difference between CreateProperty vs PropertyGroup?
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.
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.
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
andOutput
attributes to theTarget
element and changeTaskParameter="Value"
toTaskParameter="ValueSetByTask"
. The latter change would preventDogSound
from being set to "Bark" when targetB
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.)
精彩评论