问题
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