MSBuild ItemGroup with condition

丶灬走出姿态 提交于 2019-12-07 01:00:41

问题


I don't know if ItemGroup is the right type to use. I will get 4 different booleans that will be true or false depending on choice.

I would like to fill up an ItemGroup with this "strings" depending on the true or false. Is that possible or what should I use?

Example

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

My ItemGroup should then have Anders and Gustaf.

Is that possible or how should I solve that?


回答1:


Since you have a bunch of items, it would be better to store them in an ItemGroup from the start since after all that is what it is meant for and it also allows transformations etc. For example this achieves what you want:

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

However if they must be properties, I do not think there is another way than enumerating them all manually like so:

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


来源:https://stackoverflow.com/questions/13010851/msbuild-itemgroup-with-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!