- | rssFeed | My book on MSBuild and Team Build | Archives and Categories Friday, 28 March 2008

MSBuild Batching Part 3

This post is to supplement the two previous posts that I have focused on batching those are located at:

Batching continues to be confusing and I hope that that post will help to further clarify it a little bit. In the two previous posts I think that I was "hiding" what was really happening by using built in metadata. This time I will expose what's happening by a simple example that only uses custom metadata. The first thing to know about batching, it always occurs over metadata, either Well Known or custom. Take a look at the project file shown below, Batching03.proj.

<!—

© Copyright 2008, Sayed Ibrahim Hashimi (sayed.hashimi@gmail.com)

-->

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

<ItemGroup>

<Server Include="Server1">

<Type>2008</Type>

<Name>SVR01</Name>

</Server>

<Server Include="Server2">

<Type>2003</Type>

<Name>SVR02</Name>

</Server>

<Server Include="Server3">

<Type>2008</Type>

<Name>SVR03</Name>

</Server>

<Server Include="Server4">

<Type>2003</Type>

<Name>SVR04</Name>

</Server>

</ItemGroup>

 

<Target Name="TaskBatching">

<Message Text="%40(Server->'%(Name)'): @(Server->'%(Name)')" />

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

<!--

Task Batching Here over the Name metadata.

Notice that Message task is invoked once per unique batch

-->

<Message Text="%25(Server.Name): %(Server.Name)" />

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

<Message Text="%25(Server.Type): %(Server.Type)" />

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

</Target>

 

<Target Name="TargetBatching01" Outputs="%(Server.Name)">

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

<Message Text="%25(Server.Name): %(Server.Name)" />

<Message Text="%25(Server.Type): %(Server.Type)" />

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

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

</Target>

<Target Name="TargetBatching02" Outputs="%(Server.Type)">

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

<Message Text="%25(Server.Name): %(Server.Name)" />

<Message Text="%25(Server.Type): %(Server.Type)" />

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

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

</Target>

 

 

<Target Name="All" DependsOnTargets="TaskBatching;TargetBatching01;TargetBatching02"/>

</Project>

There are three targets that are important TaskBatching, which demonstrates how you can invoke individual tasks once per batch and then two targets; TargetBatching01 & TargetBatching02, which demonstrate batching an entire target once per batch. When you want to proform batching you will always use the %(…) notation somewhere. In Task batching this will be notated only inside of the tasks' parameters. In target batching this notation will be declared in the targets Outputs attribute.

Zooming in on TaskBatching target here are the results and we will discuss

The comments on the image pretty much describe what's happening. In the first execution there we are not batching. So the item is transformed into a string and passed into the Message task. The transformation is described by what is contained in the ->(). So the Metadata Name for each element in Server is evaluated and a string is created from that by concatenating each of these values. If there were duplicate values then the duplicates would show up in the resulting string.

Moving on to the next one, we batch on the Name Metadata. Since there were 4 Name metadata values the Message task is invoked 4 distinct times. The same applies for the Next invocation of the Message task.

Now let's take a look at the Target Batching. I simply invoked these targets, nothing special. The TargetBatching01 target is batching on the Name Metadata, and the TargetBatching02 on the Type Metadata. The output is shown below.

Because there are 4 distinct Name values TargetBatching is invoked 4 times, and similarily TargetBatching twice because of the distinct Type values. So if you just need to perform a single task over a set of parameters, the use Task batching. But If you need to perform a set of actions on a set of parameters then use Target Batching. More to come later on this topic I'm sure.

As a note I have decided to resume work on the MSBuild book I referenced in earlier posts. You'll be happy to know that I will cover batching in great detail there.

 

Sayed Ibrahim Hashimi

 

 

 

 

 

 

ddd

Friday, 28 March 2008 20:04:53 (GMT Standard Time, UTC+00:00)  #     |