<ItemGroup>
<!-- Unit Test Projects-->
<MyGroup Include="Hello.xml" />
<MyGroup Include="GoodBye.xml" />
</ItemGroup>
如何创建迭代此列表并执行某项操作的任务?
<XmlPeek XmlInputPath="%(MyGroup.Identity)"
Query="/results">
<Output TaskParameter="Result"
ItemName="myResult" />
</XmlPeek>
如果myresult里面有某个文本,我想发一条错误信息。然而,对于我的生活,我无法弄清楚如何在Msbuild中遍历数组...任何人都知道如何实现这一目标?
答案 0 :(得分:29)
您需要使用批处理。批处理将基于元数据键迭代一组项目。我在http://sedotech.com/Resources#batching处汇集了大量材料。例如,看看这个简单的MSBuild文件。
<Project DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Files Include="one.txt"/>
<Files Include="two.txt"/>
<Files Include="three.txt"/>
<Files Include="four.txt"/>
</ItemGroup>
<Target Name="Demo">
<Message Text="Not batched: @(Files->'%(Identity)')"/>
<Message Text="========================================"/>
<Message Text="Batched: %(Files.Identity)"/>
</Target>
</Project>
构建演示目标时,结果为
Not batched: one.txt;two.txt;three.txt;four.txt
========================================
Batched: one.txt
Batched: two.txt
Batched: three.txt
Batched: four.txt
批处理始终使用语法%(Xyz.Abc)
。仔细查看这些链接,了解有关批处理的更多信息,然后您就会想知道。
答案 1 :(得分:17)
您可以在内部目标上使用batching,例如:
<ItemGroup>
<!-- Unit Test Projects-->
<MyGroup Include="Hello.xml" />
<MyGroup Include="GoodBye.xml" />
</ItemGroup>
<Target Name="CheckAllXmlFile">
<!-- Call CheckOneXmlFile foreach file in MyGroup -->
<MSBuild Projects="$(MSBuildProjectFile)"
Properties="CurrentXmlFile=%(MyGroup.Identity)"
Targets="CheckOneXmlFile">
</MSBuild>
</Target>
<!-- This target checks the current analyzed file $(CurrentXmlFile) -->
<Target Name="CheckOneXmlFile">
<XmlPeek XmlInputPath="$(CurrentXmlFile)"
Query="/results/text()">
<Output TaskParameter="Result" ItemName="myResult" />
</XmlPeek>
<!-- Throw an error message if Result has a certain text : ERROR -->
<Error Condition="'$(Result)' == 'ERROR'"
Text="Error with results $(Result)"/>
</Target>