Copy Item's Metadata in msbuild

落爺英雄遲暮 提交于 2019-12-22 01:19:49

问题


I am creating Item B based on item A, and would like to copy all of A's metadata to B (and add some additional meta data).

<ItemGroup>
  <B Include="@A">
    <M1>%(A.M1)</M1>
    <M2>%(A.M2)</M2>
    <M3>%(A.M3)</M3>
    ...
    <M100>%(A.M100)</M100>
    ... Additional metadata specific to B ...
  </B>
</ItemGroup>

Instead of copying each metadata M1 - M100 individually from A to B, is it possible to tell msbuild to copy all metadata from A to B?
Could such a "batch metadata copy" be conditioned?

Something like:

<ItemGroup>
  <B Include="@A">        
    ... Additional metadata specific to B ...
  </B>
</ItemGroup>
<CopyMetadata From="@A" To="@B" Condition="... Check something ..."/>

Thanks.


回答1:


When you copy items, its metadata is copied too. See the working example for MSBuild v4.0:

<Project DefaultTargets="DoSomethingWithB" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <A Include="1">
      <M1>M1 (1)</M1>
      <M2>M2 (1)</M2>
      <M3>M3 (1)</M3>
      <N4>HERE</N4>
    </A>
    <A Include="2">
      <M1>M1 (2)</M1>
      <M2>M2 (2)</M2>
      <M3>M3 (2)</M3>
    </A>
  </ItemGroup>

  <Target Name="PrepareB" Outputs="%(A.Identity)">
     <ItemGroup>
       <B Include="@(A)">
         <M4>M4 (%(A.Identity))</M4>
         <M5 Condition="'%(A.N4)'!=''">M5 (%(A.Identity) for A.N4 != '')</M5>
       </B>
     </ItemGroup>      
  </Target>

  <Target Name="DoSomethingWithB"
          DependsOnTargets="PrepareB">

     <Message Text="ItemGroup A" />
     <Message Text="%(A.Identity): M1=%(A.M1), M2=%(A.M2), M3=%(A.M3), N4=%(A.N4)" />

     <Message Text="ItemGroup B" />
     <Message Text="%(B.Identity): M1=%(B.M1), M2=%(B.M2), M3=%(B.M3), N4=%(B.N4), M4=%    (B.M4), M5=%(B.M5)" />
  </Target>
</Project>

Output:

ItemGroup A
1: M1=M1 (1), M2=M2 (1), M3=M3 (1), N4=HERE
2: M1=M1 (2), M2=M2 (2), M3=M3 (2), N4=
ItemGroup B
1: M1=M1 (1), M2=M2 (1), M3=M3 (1), N4=HERE, M4=M4 (1), M5=M5 (1 for A.N4 != '')
2: M1=M1 (2), M2=M2 (2), M3=M3 (2), N4=, M4=M4 (2), M5=



来源:https://stackoverflow.com/questions/5165476/copy-items-metadata-in-msbuild

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!