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 ]

Comments (11)

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

    James…

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

  3. Mike Fourie says:

    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

  4. Mike says:

    Please… someone care to explain why for others of us who are not quite so sharp today? 🙂

  5. Mike Fourie says:

    Good question Mike 🙂 I’ll let Faisal do it since it still hurts my brain *grin*.

    Neil

  6. I guess ‘@(GroupA)’ != ” is redundant because we’re iterating groupA anyway.

  7. Shawn says:

    Why not use a condition like this:

    "’%(Identity)’ == ‘@(GroupB)’"

    Which seems like an easier way to describe what’s going on.

  8. Laurius says:

    Hey,

    I am new to MS build.  And I have a question: where was identity defined?  What does %(identity) mean?  Thanks

  9. So, as some of you pointed out, the batching brainteaser&amp;nbsp;does print out the &quot;intersection&quot; of GroupA…

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

  11. Droll Quip says:

    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?