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="https://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
Anonymous
May 18, 2006
Brian Harry gives an excellent post on Deploying TFS in the Developer Division at Microsoft.
James...Anonymous
May 18, 2006
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.Anonymous
May 18, 2006
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).
NeilAnonymous
May 18, 2006
Please... someone care to explain why for others of us who are not quite so sharp today? :-)Anonymous
May 19, 2006
Good question Mike :) I'll let Faisal do it since it still hurts my brain grin.
NeilAnonymous
May 21, 2006
I guess '@(GroupA)' != '' is redundant because we're iterating groupA anyway.Anonymous
May 24, 2006
Why not use a condition like this:
"'%(Identity)' == '@(GroupB)'"
Which seems like an easier way to describe what's going on.Anonymous
May 25, 2006
Hey,
I am new to MS build. And I have a question: where was identity defined? What does %(identity) mean? ThanksAnonymous
May 29, 2006
So, as some of you pointed out, the batching brainteaser&nbsp;does print out the "intersection" of GroupA...Anonymous
August 22, 2007
Last time , I created an installer with a shortcut in the user's start menu to a very simple application.Anonymous
June 10, 2009
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?