使用 MSBuild 合并两个项目组

Merging two item groups using MSBuild

其实我有两个问题:

  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 更令人困惑而不是有用:))

谢谢!

这是所有标准的 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>

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