【问题标题】:Merging two item groups using MSBuild使用 MSBuild 合并两个项目组
【发布时间】:2016-01-27 18:57:37
【问题描述】:

我其实有两个问题:

  1. 如何按扩展名过滤文件列表(在 ItemGroup 中)?

  2. 我有两个通过 XMLPeek 从 XML 文件中读取的文件列表,我想根据条件将它们合并在一起。这可以在 MSBuild 中完成吗?

目前,我有这样的事情:

<Target Name="GetExportFileList">

    <!-- Can't use XPath to filter the value by extension because MSBuild doesn't support XPath 2.0's ends-with() function -->
    <XmlPeek XmlInputPath="$(XmlFile)"
             Query = "//File[not(./@value = '') and not(./@value = preceding::File/@value)]/@value">

        <!-- this file list will have files with two different extensions, we'll call them .ext1 and ext2 files - need a way to split this into two ItemGroups each containing only one of the file types --> 
        <Output TaskParameter="Result"
                ItemName="ReferencedFiles" />
    </XmlPeek>


    <!-- .ext2 files are XML and contain references to other .ext1 files -->
    <XmlPeek XmlInputPath="$(DirectlyReferencedExt2Files)"
             Query="//Ext1[not(./@FilePath = '')]/@FilePath">

             <Output TaskParameter="Result"
             ItemName="IndirectlyReferencedExt1Files" />
    </XmlPeek>

    <!-- combine the two lists -->
    <ItemGroup>
        <AllExt1Files Include="@IndirectlyReferencedExt1Files" />
        <AllExt1Files Include="@DirectlyReferencedExt1Files" Exclude="@AllExt1Files" />            
    </ItemGroup>
</Target>

回顾一下:

  1. 我需要一种方法来将所有 .ext1 和 .ext2 类型的文件分别从 ReferencedFiles 提取到 DirectlyReferencedExt1Files 和 DirectlyReferencedExt2Files(因为我不能在 MSBuild 中使用 XPath 2.0 查询)
  2. 将 DirectlyReferencedExt1Files 和 IndirectlyReferencedExt1Files(从 DirectlyReferencedExt2Files 中提取)合并到一个列表中进行处理。

可能有一些非常简单的方法可以做到这一点,但我无法理解 MSBuild 的工作原理以及在哪里可以找到我正在尝试做的事情的参考(MSDN 比有用更令人困惑:))

谢谢!

【问题讨论】:

标签: merge msbuild filtering file-type


【解决方案1】:

这是所有标准的 msbuild 功能:

<ItemGroup>
  <ReferencedFiles Include="a.ext1;b.ext1;c.ext2;d.ext2"/>
</ItemGroup>

<Target Name="FilterIt">
  <ItemGroup>
    <DirectlyReferencedExt1Files Include="%(ReferencedFiles.Identity)" Condition="'%(Extension)'=='.ext1'" />
    <DirectlyReferencedExt2Files Include="%(ReferencedFiles.Identity)" Condition="'%(Extension)'=='.ext2'" />
    <AllOfThem Include="@(DirectlyReferencedExt1Files);@(DirectlyReferencedExt2Files)" />
  </ItemGroup>
  <Message Text="@(DirectlyReferencedExt1Files)" />
  <Message Text="@(DirectlyReferencedExt2Files)" />
  <Message Text="@(AllOfThem)" />
</Target>

注意:您的问题是重复的,但是因为您问了其中两个问题,所以很难找到 exact SO要求的重复问题。例如,请参阅 How do you filter an ItemGroup?MSBuild ItemGroup with conditionHow can I merge two ItemGroups in MSBuild

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多