【问题标题】:How can I merge two ItemGroups in MSBuild如何在 MSBuild 中合并两个 ItemGroup
【发布时间】:2012-09-16 21:03:19
【问题描述】:

我有两个 ItemGroups:

<ItemGroup>
    <Compile Include="A.cs" />
    <Compile Include="B.cs" />
    <Compile Include="C.cs" />
</ItemGroup>
<ItemGroup>
    <PEG Include="A.cs" />
    <PEG Include="Y.cs" />
    <PEG Include="Z.cs" />
</ItemGroup>

我需要将PEG 中的每个项目添加到Compile,如果且仅当该项目不在Compile 中时。

有没有简单的方法可以做到这一点?

我的第一次尝试是这样的:

<ItemGroup>
  <Compile Include="%(PEG.Identity)" Condition=" '$(Compile)' != '%(PEG.Identity)' " />
</ItemGroup>

但由于显而易见的原因,这不起作用。我真的不想在 MSBuild 中进行手动反连接。

也许使用Exclude 会起作用?

【问题讨论】:

    标签: .net build msbuild compilation


    【解决方案1】:

    好吧,我想通了。

    我做错的第一件事是使用$(Compile) 而不是@(Compile)

    接下来,我在 Exclude 属性上走上了正轨。

    这是我想出的:

    <?xml version="1.0" encoding="utf-8" ?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Compile">
      <ItemGroup>
        <Compile Include="A.cs" />
        <Compile Include="B.cs" />
        <Compile Include="C.cs" />
      </ItemGroup>
      <Target Name="Compile">
        <ItemGroup>
            <PEG Include="A" />
            <PEG Include="D" />
        </ItemGroup>
        <Message Text="Compile = @(Compile)" />
        <Message Text="PEG     = @(PEG)" />
        <ItemGroup>
            <Compile Include="%(PEG.Identity).cs" Exclude="@(Compile)" />
        </ItemGroup>
        <Message Text="Compile = @(Compile)" />
      </Target>
    </Project>
    

    完成此操作后,Compile 将根据需要包含A.cs;B.cs;C.cs;D.cs

    【讨论】:

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