【问题标题】:Create an ItemGroup of strings in MSBuild在 MSBuild 中创建字符串的 ItemGroup
【发布时间】:2010-08-17 00:07:52
【问题描述】:

我想创建一个任意字符串/名称的“ItemGroup”,以便使用 MSBuild 转换,例如:

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

然后我希望将这些类别的转换传递到控制台应用程序中,例如:

/c @(Categories, ' /c ')

我之所以在引号中说“ItemGroup”,是因为我不确定以这种方式使用 ItemGroups 是否适用于我——据我所知,文档中没有任何内容表明 ItemGroups 必须是文件,但是由于缺少强制性的“包含”属性,使用上述方法会导致错误消息。

  • 有没有办法使用 ItemGroups 进行上述操作?
  • 或者,有没有更好的方法来实现上述目标而不使用 ItemGroups?

【问题讨论】:

    标签: msbuild itemgroup


    【解决方案1】:

    您可以使用任意字符串以及Item 中的文件,但您必须使用以下语法:

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

    Item 与任意字符串一起使用时的唯一区别是某些元数据将毫无意义。 (例如%(Categories.FullPath)

    然后您可以使用您的项目执行如下命令:

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

    【讨论】:

    • +1,但请注意 - @(Categories, ' /c ') 表示“ /c ”将是项目之间的 分隔符,因此您的 LAST 项目将丢失/c (或第一个取决于您的 POV)。你真的需要一个像这样的 transform:@(Categories -> '/c %(Identity)') 让 /c 在你所有的项目前面
    • 是的,你是对的,但这就是他使用 '/c @(Categories, ' /c ')' 的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多