【问题标题】:msbuild create itemgroup from property groupmsbuild 从属性组创建 itemgroup
【发布时间】:2016-06-16 13:22:28
【问题描述】:

我想传递一个以分号分隔的字符串列表。
每个字符串代表一个文件名。

    <PropertyGroup>
          <FileNames>Newtonsoft.Json;Reactive</FileNames>
          <PathToOutput>C:/</PathToOutput>
    </PropertyGroup>

现在我想创建一个项目组,它应该为我提供特定文件夹中的所有文件,不包括文件名列表,例如:

<ItemGroup>
    <ReleaseFiles Include="$(PathToOutput)\**\*.*" Exclude="%(identity)-> identity.contains(%FileNames)"/>
</ItemGroup>

如果文件名变量中包含任何一个文件名,我如何遍历当前文件夹的文件并匹配每个文件名。

【问题讨论】:

    标签: msbuild teamcity msbuild-itemgroup


    【解决方案1】:

    很确定这是重复的,但我现在找不到它,所以这里是:

    • 将分号分隔的属性转换为项目只需使用Include=$(Property)
    • Exclude 仅在您有完全匹配列表时才有效,但您需要在这里进行更多任意过滤,因此您需要 Condition
    • 通过使这些 FileNames 元数据成为 ReleaseFiles 项,将两个 ItemGroup 像交叉产品一样连接在一起。然后您可以遍历 ReleaseFiles 中的每个项目并同时访问 FileNames
    • Contains 是一个属性函数(嗯,或者 System::String 方法),因此在元数据上不起作用,因此我们首先将元数据转换为字符串

    在代码中:

    <PropertyGroup>
      <FileNames>Newtonsoft.Json;Reactive</FileNames>
      <PathToOutput>C:/</PathToOutput>
    </PropertyGroup>
    
    <Target Name="FilterBasedCommaSeperatedProperty">
      <ItemGroup>
        <!-- property -> item -->
        <Excl Include="$(FileNames)"/>
        <!-- list all and add metadata list -->
        <AllReleaseFiles Include="$(PathToOutput)\**\*.*">
          <Excl>%(Excl.Identity)</Excl>
        </AllReleaseFiles >
        <!-- filter to get list of files we don't want -->
        <FilesToExclude Include="@(AllReleaseFiles)"
                        Condition="$([System.String]::Copy('%(FileName)').Contains('%(Excl)'))"/>
        <!-- all but the ones to exclude --> 
        <ReleaseFiles Include="@(AllReleaseFiles)" Exclude="@(FilesToExclude)"/>
      </ItemGroup>
      <Message Text="%(ReleaseFiles.Identity)" />
    </Target>
    

    【讨论】:

      【解决方案2】:

      通过使用排除属性并引用另一个项目组,使用标准方式从项目组中排除文件文件。理解起来会容易很多。

      例子:

      <PropertyGroup>
        <PathToOutput>C:/</PathToOutput>
      </PropertyGroup>
      
      <ItemGroup>
          <FilesToExclude Include="$(PathToOutput)\**\Newtonsoft.Json" />
          <FilesToExclude Include="$(PathToOutput)\**\Reactive" />
          <ReleaseFiles Include="$(PathToOutput)\**\*.*" Exclude="@(FilesToExclude)"/>
      </ItemGroup>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-12
        • 1970-01-01
        • 1970-01-01
        • 2012-10-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多