已加入项目组的MSBuild元数据

时间:2011-01-18 07:51:56

标签: collections msbuild itemgroup

我想加入两个项目组:

  <ItemGroup>
<ServerTypeA Include="ServerA;ServerB;">
  <MetaDataA>A</MetaDataA>
</ServerTypeA>
</ItemGroup>

  <ItemGroup>
<ServerTypeB Include="ServerB;ServerC;">
  <MetaDataB>B</MetaDataB>
</ServerTypeB>
</ItemGroup>

使用常规联接会给我一个包含4个项目的集合:

ServerA with Metadata A;
ServerB with Metadata A;
ServerB with Metadata B;
ServerC with Metadata B;

如何创建以下集合:

ServerA with Metadata A
ServerB with Metadata A & B
ServerC with Metadata B

1 个答案:

答案 0 :(得分:10)

这是可能的。您必须可以手动执行加入。

以下是如何操作的示例(需要msbuild 3.5或更高版本):

<ItemGroup>
  <ServerTypeA Include="ServerA;ServerB;">
    <MetaDataA>A</MetaDataA>
  </ServerTypeA>

  <ServerTypeB Include="ServerB;ServerC;">
    <MetaDataB>B</MetaDataB>
  </ServerTypeB>
</ItemGroup>

<Target Name="JoinServers" DependsOnTargets="ProcessServerTypeA;ProcessServerTypeB">
  <Message Text="%(Joined.Identity) Metadata: %(Joined.MetaDataA)%(Joined.MetaDataB)"/>
</Target>

<!--Create -->
<Target Name="ProcessServerTypeA">
  <ItemGroup>
    <Joined Include="%(ServerTypeA.Identity)">
      <MetaDataA>%(ServerTypeA.MetaDataA)</MetaDataA>
    </Joined>
  </ItemGroup>
</Target>

<!--Need to batch at the target level for this to work-->
<Target Name="ProcessServerTypeB" Inputs="@(ServerTypeB)" Outputs="%(ServerTypeB.Identity)'">

  <PropertyGroup>
    <!--Create Temporary Properties for the Item Metadata-->
    <TempItemName>%(ServerTypeB.Identity)</TempItemName>
    <TempMetaDataB>%(ServerTypeB.MetaDataB)</TempMetaDataB>
    <!--Does the current item already exist?-->
    <TempIsDuplicate Condition="'%(Joined.Identity)' == '$(TempItemName)'">True</TempIsDuplicate>
  </PropertyGroup>

  <ItemGroup>
    <!--Update the existing item's metadata if this is a duplicate-->
    <!--Don't provide the include attribute.  This will allow you to update existing items metadata-->
    <!--Have to reference %(Joined.Identity) in the condtion to ensure we only update the correct item-->
    <!--You cannot directly reference metadata from ServerTypeB here.  Hence the need for the temp Properties-->
    <Joined Condition="'%(Joined.Identity)' == '$(TempItemName)'">
      <MetaDataB>$(TempMetaDataB)</MetaDataB>
    </Joined>

    <!--Create a new item if current item is not a duplicate-->
    <Joined Include="$(TempItemName)" Condition="'$(TempIsDuplicate)' != 'True'">
      <MetaDataB>$(TempMetaDataB)</MetaDataB>
    </Joined>
  </ItemGroup>
</Target>

运行JoinServers目标将产生以下输出:

ServerA Metadata: A
ServerB Metadata: AB
ServerC Metadata: B

以更好的答案更新

This问题向我指出了一个更简单的解决方案。

基本上,您使用Transform modifiers%(Identity)来执行加入。

您可以使用以下内容替换上面的所有3个目标,以获得相同的输出。

<Target Name="JoinServers">
  <ItemGroup>
    <Joined Include="%(Identity)">
      <MetaDataA>@(ServerTypeA->'%(MetaDataA)')</MetaDataA>
      <MetaDataB>@(ServerTypeB->'%(MetaDataB)')</MetaDataB>
    </Joined>
  </ItemGroup>

  <Message Text="%(Joined.Identity) Metadata: %(Joined.MetaDataA)%(Joined.MetaDataB)"/>
</Target>