這是爲了補充而不是替代,@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