【问题标题】:msbuild array iterationmsbuild 数组迭代
【发布时间】:2010-06-15 13:57:55
【问题描述】:
<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 中遍历数组......有人知道如何完成这个吗?

【问题讨论】:

    标签: msbuild


    【解决方案1】:

    您需要为此使用批处理。批处理将基于元数据键迭代一组项目。我在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>
    

    当您构建 Demo 目标时,结果是

    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]First(),但我无法让它工作。
    【解决方案2】:

    你可以在内部目标上使用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>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 2013-01-16
    • 2020-03-29
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多