Create an ItemGroup of strings in MSBuild

孤者浪人 提交于 2019-12-12 04:44:54

问题


I want to create an "ItemGroup" of arbitrary strings / names in order to use MSBuild transforms, for example:

<ItemGroup>
    <Categories>First</Categories>
    <Categories>Second</Categories>
</ItemGroup>

I then wish to pass a transform of these categories into a console app, e.g.:

/c @(Categories, ' /c ')

The reason why I say "ItemGroup" in quotes, is because I'm not sure whether or not it is applicable for me to use ItemGroups in this way - as far as I can see nothing in the documentation states that ItemGroups must be files, however using the above results in an error message due to missing mandatory "Include" attribute.

  • Is there a way of doing the above using ItemGroups?
  • Alternatively is there a better way of achieving the above without using ItemGroups?

回答1:


You can use arbitrary string as well as files in Item, but you must use this syntax :

<ItemGroup>
  <Categories Include="First"/>
  <Categories Include="Second"/>
</ItemGroup>

The only difference when you use Item with arbitrary string is that some metadata will be meaningless. (%(Categories.FullPath) for example)

You can then use your Item to execute a command like this :

<Target Name="ExecCommand">
  <Exec Command="YourProgram.exe /c @(Categories, ' /c ')"/>

  <!-- Using transformation -->
  <Exec Command="YourProgram.exe @(Categories -> '/c %(Identity)')"/>
</Target>


来源:https://stackoverflow.com/questions/3498374/create-an-itemgroup-of-strings-in-msbuild

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