Welcome to MSDN Blogs Sign in | Join | Help

What Does This MSBuild Snippet Do?

It's been a while since we've had a little quiz on MSBuild.

Over the last couple of days the MSBuild team has been enjoying working up solutions to a question over in our forums. Last night Faisal and Jeff concoted some MSBuild script that, to be honest, made my head turn sideways and then inside out.

Given the following MSBuild file, what is the output of the build?

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <
ItemGroup
>
        <
GroupA Include="file1.txt"
/>
        <
GroupA Include="file2.txt"
/>
        <
GroupA Include="file3.txt"
/>
        <
GroupA Include="file4.txt"
/>
    </
ItemGroup
>

    <
ItemGroup>
        <
GroupB Include="file1.txt"
/>
        <
GroupB Include="file3.txt"
/>
        <
GroupB Include="file5.txt"
/>
    </
ItemGroup
>

    <
Target Name="Build"
>
        <
CreateItem Include="@(GroupA)" Condition="'%(Identity)' != '' and '@(GroupA)' != '' and '@(GroupB)' != ''"
>
            <
Output TaskParameter="Include" ItemName="GroupC"
/>
       </
CreateItem
>
       <
Message Text="@(GroupC)"
/>
    </
Target
>
</
Project>

No running this on the command-line to find out! That'd be cheating :) See if you can figure it out in your noggin first, and then post your thoughts.

[ Author: Neil Enns ]

Published Thursday, May 18, 2006 6:35 PM by msbuild
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Thursday, May 18, 2006 3:49 PM by Team System News

# VSTS Links - 05/18/2006

Brian Harry gives an excellent post on Deploying TFS in the Developer Division at Microsoft.

James...
Thursday, May 18, 2006 4:57 PM by KeithH

# re: What Does This MSBuild Snippet Do?

I imagine that it would print out the items that are common to both GroupA and GroupB i.e.:

file1.txt;file3.txt

BTW it is hard to tell the single quotes from the double quotes so it would be easier to read the condition if you could display the XML in a fixed pitch font.
Friday, May 19, 2006 1:27 AM by msbuild

# re: What Does This MSBuild Snippet Do?

Well done Keith! It prints the intersection of the two item groups. You're right, too, about the code being hard to read. I've changed it to Courier New.

Now for the bonus question: which part of that condition statement is actually redundant, and why? (Rajeev pointed this out when we asked the MSBuild team about this code).

Neil
Friday, May 19, 2006 4:32 AM by Mike

# re: What Does This MSBuild Snippet Do?

Please... someone care to explain why for others of us who are not quite so sharp today? :-)
Friday, May 19, 2006 1:26 PM by msbuild

# re: What Does This MSBuild Snippet Do?

Good question Mike :) I'll let Faisal do it since it still hurts my brain *grin*.

Neil
Sunday, May 21, 2006 5:10 PM by Thomas Freudenberg

# re: What Does This MSBuild Snippet Do?

I guess '@(GroupA)' != '' is redundant because we're iterating groupA anyway.
Wednesday, May 24, 2006 11:56 AM by Shawn

# re: What Does This MSBuild Snippet Do?

Why not use a condition like this:
"'%(Identity)' == '@(GroupB)'"

Which seems like an easier way to describe what's going on.
Thursday, May 25, 2006 1:09 PM by Laurius

# re: What Does This MSBuild Snippet Do?

Hey,

I am new to MS build.  And I have a question: where was identity defined?  What does %(identity) mean?  Thanks
Tuesday, May 30, 2006 12:39 AM by MSBuild Team Blog

# Batching Brainteaser Explained

So, as some of you pointed out, the batching brainteaser&amp;nbsp;does print out the &quot;intersection&quot; of GroupA...
Wednesday, August 22, 2007 4:15 PM by Re-inventing The Wheel

# Select-Play-Select-3-0-Select

Last time , I created an installer with a shortcut in the user's start menu to a very simple application.

Wednesday, June 10, 2009 11:33 AM by Droll Quip

# re: What Does This MSBuild Snippet Do?

Is it possible to disable batching in MsBuild?  If you want an ItemGroup with meta-data in the order specified, how can this be done?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker