MSBUILD ITEM group具有条件



我不知道ItemGroup是否是正确使用的类型。我将获得4种不同的布尔值,这取决于选择。

我想根据True或False填充此"字符串"的ItemGroup。那是可能的还是我应该使用什么?

示例

Anders = true
Peter = false
Michael = false
Gustaf = true

我的ItemGroup应该有安德斯和古斯塔夫。

是可能的还是我应该如何解决?

由于您有很多项目,因此最好从一开始就将它们存储在ItemGroup中,因为毕竟所有内容是用来的,并且还允许转换等。示例这实现了您想要的:

<ItemGroup>
  <Names Include="Anders">
    <Value>True</Value>
  </Names>
  <Names Include="Peter">
    <Value>False</Value>
  </Names>
  <Names Include="Michael">
    <Value>False</Value>
  </Names>
  <Names Include="Gustaf">
    <Value>True</Value>
  </Names>
</ItemGroup>
<Target Name="GetNames">
  <ItemGroup>
    <AllNames Include="%(Names.Identity)" Condition="%(Names.Value)==true"/>
  </ItemGroup>
  <Message Text="@(AllNames)"/>  <!--AllNames contains Anders and Gustaf-->
</Target>

但是,如果它们必须是属性,我认为没有其他方法,而是手动列举它们。

<PropertyGroup>
  <Anders>True</Anders>
  <Peter>False</Peter>
  <Michael>False</Michael>
  <Gustaf>True</Gustaf>
</PropertyGroup>
<Target Name="GetNames">
  <ItemGroup>
    <AllNames Include="Anders" Condition="$(Anders)==true"/>
    <AllNames Include="Peter" Condition="$(Peter)==true"/>
    <AllNames Include="Michael" Condition="$(Michael)==true"/>
    <AllNames Include="Gustaf" Condition="$(Gustaf)==true"/>
  </ItemGroup>
  <Message Text="@(AllNames)"/>
</Target>

最新更新