我怎样才能在合并的MSBuild 2 ItemGroups

问题描述:

我有两个ItemGroups:

I have two 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 做添加的每个项目,以编译当且仅当该项目不AREADY在编译

I need to do add each of the items in PEG to Compile IF AND ONLY IF that item is not AREADY in Compile.

有没有一种简单的方法来做到这一点?

Is there an easy way to do that?

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

My first attempt is something like:

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

但是,这并不显而易见的原因工作。我真的不希望做一个人工抗加入的MSBuild。

But that doesn't work for obvious reasons. I don't really want to do a manual anti-join in MSBuild.

或许,使用排除将工作?

好吧,我想它了。

我做错的第一件事,是用 $(编译),而不是 @(编译)

The first thing I was doing wrong, was using $(Compile) rather than @(Compile).

接下来,我是在正确的轨道与排除属性上。

Next, I was on the right track with the Exclude property.

下面是我想出了:

<?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>

完成之后,编译包含 A.cs; B.cs; C.cs; D.cs 根据需要。