2010-06-15 94 views
15
<ItemGroup> 
    <!-- Unit Test Projects--> 
    <MyGroup Include="Hello.xml" /> 
    <MyGroup Include="GoodBye.xml" />  
</ItemGroup> 

如何製作一個迭代通過此列表並執行某項操作的任務?msbuild數組迭代

<XmlPeek XmlInputPath="%(MyGroup.Identity)" 
     Query="/results"> 
    <Output TaskParameter="Result" 
      ItemName="myResult" /> 
</XmlPeek> 

如果myresult裏面有一定的文字,我想發一個錯誤信息。然而,對於我的生活,我無法弄清楚如何遍歷Msbuild中的數組......任何人都知道如何做到這一點?

回答

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> 
28

您需要使用批處理這一點。批處理將基於元數據鍵迭代一組項目。我在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)。仔細查看這些鏈接,瞭解有關批處理的更多信息,然後您想知道。

+0

如何獲取物品組的第一個物品?我用各種方法試過'[0]'和'First()',但是我無法使它工作。 – 2017-12-12 13:46:50