【问题标题】:How to reference an item in an ItemGroup based on the Label attribute?如何根据 Label 属性引用 ItemGroup 中的项目?
【发布时间】:2012-08-03 02:19:26
【问题描述】:

在我的 cpp 项目文件中,我有一个这样定义的项目组:

<ItemGroup>
<None Include="file1.dll" Label="Release Version"/>
<None Include="file2.dll" Label="Debug Version"/>
</ItemGroup>

我有一个 AfterBuild 目标,我想根据项目的标签属性将上述文件复制到不同的位置。例如:

<Target Name="AfterBuild">
<Copy SourceFiles="@(None)" DestinationFiles="$(ReleaseLocation)" Condition="'%(None.Label)'=='Release Version'" ContinueOnError="false" />
</Target>

但这不起作用(没有复制任何内容)。如何在复制命令中引用标签属性?

【问题讨论】:

    标签: msbuild label itemgroup


    【解决方案1】:

    您不能根据标签属性过滤项目,但您可以根据项目的元数据进行过滤。例如:

    <ItemGroup>
      <None Include="file1.dll">
       <Label>Release Version</Label>
      </None>
      <None Include="file2.dll">
        <Label>Debug Version</Label>
      </None>
    </ItemGroup>
    
    <Target Name="AfterBuild">
      <ItemGroup>
        <_RetailContent Include="@(None)" Condition="%(Label) == 'Release Version'" />
      </ItemGroup>
      <Copy SourceFiles="@(_RetailContent)" DestinationFolder="$(ReleaseLocation)" ContinueOnError="false" />
    </Target>
    

    【讨论】:

    • 这里的“无”代表什么?
    • @jayarjo,“无”是问题作者使用的项目组。这个组或组名没有什么特别之处,它可能也是“MyFavoriteItemGroup”或任何其他名称。
    • MS doc page 暗示标签属性应该可用于“过滤”项目:“标签属性在某些构建系统中用作控制构建行为。您只能在声明中使用它,作为创建更易于理解的 MSBuild 脚本的一种方式,或作为影响构建操作的控制设置。"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2018-10-11
    • 1970-01-01
    • 2019-09-16
    相关资源
    最近更新 更多