这是为了补充而不是替代,@BryanJ’s answer。
有两种类型的配料。一种是当您使用%(ItemName.MetaData)
语法时发生的Task batching。您只需将此值指定为任务参数,就好像%(ItemName.MetaData)
只会扩展为一个特定值。 MSBuild然后自动执行任务多次,避免任务显式支持迭代项目列表。
另一种配料类型是Target batching。当您使用<Target/>
的Inputs
和Outputs
属性时,会发生目标批处理。要以每个项目只执行一次目标的方式对任意一组项目进行批处理,您可以指定Inputs="@(ItemName)" Outputs=%(Identity).bogus
。重要的是%(Identity)
存在。批次将查看所有可能的扩展Inputs
和Outputs
并根据这些扩展来决定其批处理。因此,如果您希望目标针对每个项目单独运行,您必须确保每个项目都获得了独特的扩展。我给@ BryanJ与修改代码来使用这种风格的目标配料:
<?xml version="1.0"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="all">
<ItemGroup>
<Messages Include="Message1">
<Text>Hello from Message1</Text>
<Group>1</Group>
</Messages>
<Messages Include="Message2">
<Text>Hello from Message2</Text>
<Group>1</Group>
</Messages>
<Messages Include="Message3">
<Text>Hello from Message3</Text>
<Group>2</Group>
</Messages>
</ItemGroup>
<Target Name="all" DependsOnTargets="TestMessage;TestMessageGrouping" />
<!--
Use the Inputs/Outputs attributes to specify Target
batching. The metadata value I am batching over is
Identity. Since Identity is unique per item, this means the
Target will get run in full once for every value in
Messages. We provide something bogus for Outputs. It is
important that our bogus values do not coincide with real
filenames. If MSBuild finds a file with the name of a value
in Outputs and another file, with an older timestamp,
matching the corresponding value in Inputs, it will skip
running this Target. (This is useful in many situations, but
not when we want to just print out messages!)
-->
<Target Name="TestMessage" Inputs="@(Messages)" Outputs="%(Identity).bogus">
<Message Text="I will print the Text metadata property of %(Messages.Identity)" />
<Message Text="%(Messages.Text)" />
</Target>
<!--
If you want to combine Task and Target batching, you can specify
a different metadata value than Identity to group the items
by. I use the Group metadata I specified in the ItemGroup.
-->
<Target Name="TestMessageGrouping" Inputs="@(Messages)" Outputs="%(Group).bogus">
<Message Text="I will print the Text metadata property of all messages from Group %(Messages.Group)" />
<!--
Now, within the Target batch, we use Task batching to print
all of the messages in our %(Messages.Group) at once.
-->
<Message Text="%(Messages.Text)" />
</Target>
</Project>
与输出:
TestMessage:
I will print the Text metadata property of Message1
Hello from Message1
TestMessage:
I will print the Text metadata property of Message2
Hello from Message2
TestMessage:
I will print the Text metadata property of Message3
Hello from Message3
TestMessageGrouping:
I will print the Text metadata property of all messages from Group 1
Hello from Message1
Hello from Message2
TestMessageGrouping:
I will print the Text metadata property of all messages from Group 2
Hello from Message3
像[这](http://stackoverflow.com/q/7407400/147211) ? – KMoraz
@KMoraz不,该帖子指的是延迟名称解析。我想要做的是将多个变量传递给一个目标,然后让目标进程处理每个变量,就像一个方法一样。 – oliwa