【问题标题】:How can I prepend items in an MSBuild ItemGroup I've created by splitting a string?如何在通过拆分字符串创建的 MSBuild ItemGroup 中添加项目?
【发布时间】:2014-02-05 17:56:19
【问题描述】:

我正在解析我们从 Visual Studio 项目文件中获取的一些项目,并且需要对其中的一些文件运行命令行实用程序。我需要使用的参数基于辅助文件的集合,这些辅助文件通常存储为分号分隔的列表,存储为项目文件中的项目元数据:

<ItemGroup>
  <Content Include="js\main.js">
    <Concatenate>True</Concatenate>
    <MoreFiles>path\to\file-1.js;path\to\file-2.js;path\to-another\file-3.js;path\to-yet-another\file-4.js</MoreFiles>
  </Content>
  <Content Include="js\another.js">
    <Concatenate>True</Concatenate>
    <MoreFiles>path\to\file-5.js;path\to\file-6.js;path\to\file-7.js;path\to-another\file-8.js</MoreFiles>
  </Content>
</ItemGroup>

我目前正在使用属性 JSFiles 检索这些内容,该属性是从 @(Content) 构建的,因此:

<ItemGroup>
  <JSFiles Include="@(Content)" KeepMetadata="Concatenate;MoreFiles" Condition="'%(Content.Extension)' = '.js' AND '%(Content.Concatenate)' == 'true'" />
</ItemGroup>

然后我使用第二个目标,它使用@(JSFiles) 作为其输入:

<Target Name="ConcatenateJS" Inputs="@(JSFiles)" Outputs="%(JSFiles.Identity).concatenatetemp">
  <Message Importance="high" Text="  %(JSFiles.Identity):" />
  <PropertyGroup>
    <MoreFiles>%(JSFiles.MoreFiles)</MoreFiles>
  </PropertyGroup>
  <ItemGroup>
    <MoreFilesArray Include="$(MoreFiles.Split(';'))" />
  </ItemGroup>

  <Message Importance="high" Text="    MoreFiles: %(MoreFilesArray.Identity)" />
</Target>

到目前为止,一切都很好。至此,我可以使用&lt;Message /&gt; 任务来输出Split 操作的内容,这就是我所期望的:

ConcatenateJS:
  js\main.js:
    MoreFiles: path\to\file-1.js
    MoreFiles: path\to\file-2.js
    MoreFiles: path\to-another\file-3.js
    MoreFiles: path\to-yet-another\file-4.js
ConcatenateJS:
  js\another.js:
    MoreFiles: path\to\file-5.js
    MoreFiles: path\to\file-6.js
    MoreFiles: path\to\file-7.js
    MoreFiles: path\to-another\file-8.js

但是,为了将这些文件正确传递给命令行实用程序,它们需要是完整路径,因此我需要在 $(MoreFiles) 中的所有项目前面加上 $(MSBuildProjectDirectory)

我尝试过使用批处理操作(使用$(MSBuildProjectDirectory)\%(MoreFilesArray.Identity) 甚至$([System.IO.Path]::Combine($(MSBuildProjectDirectory), %(MoreFilesArray.Identity)) 都无济于事),我尝试使用&lt;CreateItem&gt; 使用AdditionalMetadata 属性,但这似乎不起作用对我来说太好了(虽然我不确定我是否正确使用它)。

我怎样才能使我的构建过程的输出看起来像这样:

ConcatenateJS:
  js\main.js:
    MoreFiles: C:\full\path\to\file-1.js
    MoreFiles: C:\full\path\to\file-2.js
    MoreFiles: C:\full\path\to-another\file-3.js
    MoreFiles: C:\full\path\to-yet-another\file-4.js
ConcatenateJS:
  js\another.js:
    MoreFiles: C:\full\path\to\file-5.js
    MoreFiles: C:\full\path\to\file-6.js
    MoreFiles: C:\full\path\to\file-7.js
    MoreFiles: C:\full\path\to-another\file-8.js

谢谢!

【问题讨论】:

  • ------------------ 你不需要分裂。分号是默认的。

标签: visual-studio msbuild itemgroup


【解决方案1】:

MsBuild 项目有一个名为“FullPath”的well-known metadata,它将显示项目的完整路径。

<Message Importance="high" Text="    MoreFiles: %(MoreFilesArray.FullPath)" />

【讨论】:

  • 太棒了——我曾想过,因为它使用的是从字符串本身拆分出来的字符串数组,所以这是行不通的,甚至都懒得尝试。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多