Monday, February 25, 2008

The other day I was talking to a friend of mine and he was experiencing some difficulties understanding some behavior in his build file. To summarize what was confusing him, Properties are evaluated on the fly and as files are imported. Items are gathered as files are imported then evaluated at the end of the project parsing but before any target is executed. To demonstrate this in a simple MSBuild example take a look at the simple build file example below.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="PrintInfo">

 

<PropertyGroup>

<PropBefore>[$(Configuration)]</PropBefore>

</PropertyGroup>

<ItemGroup>

<ItemBefore Include="[$(Configuration)]" />

</ItemGroup>

 

 

<PropertyGroup>

<Configuration>Debug</Configuration>

</PropertyGroup>

 

<PropertyGroup>

<PropAfter>[$(Configuration)]</PropAfter>

</PropertyGroup>

<ItemGroup>

<ItemAfter Include="[$(Configuration)]" />

</ItemGroup>

 

<Target Name="PrintInfo">

<Message Text="PropBefore: $(PropBefore)"/>

<Message Text="ItemBefore: @(ItemBefore)"/>

 

<Message Text="==============================================="/>

 

<Message Text="PropAfter: $(PropAfter)"/>

<Message Text="ItemAfter: @(ItemAfter)"/>

 

</Target>

 

</Project>

 

So here the property Configuration is defined in the middle, before and after that I have declared both a Property & an Item that contains its value. Take a look at the output below.

The noticeable difference here is that the ItemBefore actually has the correctly resolved value for the Configuration property where as the PropBefore property does not. If you think about this it makes sense because properties depend on other properties and you can change/extend the value for a property as the project is loaded. Items don't behave in this manner. Anywayz this is something that you should be careful not to trip on.

 

Sayed Ibrahim Hashimi

Monday, February 25, 2008 7:26:48 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  |  Tracked by:
"Link Listing - February 25, 2008" (Christopher Steen) [Trackback]
"Link Listing - February 25, 2008" (Christopher Steen) [Trackback]

Wednesday, March 05, 2008 1:18:23 PM (GMT Standard Time, UTC+00:00)
Cool, the post.

Thanks for the information.
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, strike, strong, sub) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

Theme design by Jelle Druyts