将项目传递给 MSBuild 任务 [英] Passing Items to MSBuild Task

查看:11
本文介绍了将项目传递给 MSBuild 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的目标中使用MSBuild"任务来构建另一个项目,同时将一些项目(及其元数据)从当前项目传递到要构建的项目.
虽然可以使用 Properties 属性或 AdditionalProperties 元数据来传递属性,但我找不到传递项目的方法.
一种可能的解决方案是将项目写入文件并将文件名作为属性传递,但这只会传递项目,而不传递它们的元数据.

I would like to use the "MSBuild" task in my target in order to build another project, while passing some items (with their metadata) from the current project to the project to be built.
While it's possible to pass properties using the Properties attribute or the AdditionalProperties Metadata, I couldn't find a way to pass Items.
A possible solution could be to write the items to a file and pass the filename as a property, but this would pass only the items, without their metadata.

有什么想法吗?

谢谢.

推荐答案

编写一个自定义任务来将项目及其元数据转储到一个文件中,以便由另一个进程获取是相当简单的.但是,不要只是以原始文本形式转储项目,而是生成一个包含项目组(带有项目元数据)的有效 MSBuild 项目文件,并将生成的文件导入由 MSBuild 任务执行的项目.您甚至可以使用 MSBuild 4.0 内联任务转储文件.

It is fairly straightforward to write a custom task to dump items and their metadata to a file, to be picked up by another process. Instead of just dumping the items in raw text form though, generate a valid MSBuild project file containing the item group (with item meta data) and have that generated file imported by the project being executed by the MSBuild task. You can even use an MSBuild 4.0 inline task to dump the file.

(回复评论)

<UsingTask
  TaskName="WriteItemsWithMetadata"
  TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)Microsoft.Build.Tasks.v4.0.dll" >
  <ParameterGroup>
    <OutputFile ParameterType="System.String" Required="true" />
    <Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
  </ParameterGroup>
  <Task>
    <Using Namespace="System.IO" />
    <Code Type="Fragment" Language="cs">
  <![CDATA[
  // This code simplified with specific knowledge
  // of the item metadata names.  See ITaskItem
  // documentation to enable writing out arbitrary
  // meta data values
  //
  using (StreamWriter writer = new StreamWriter(OutputFile))
  {
    writer.Write("<?");
    writer.WriteLine(" version="1.0" encoding="utf-8"?>");
    writer.WriteLine("<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"");
    writer.WriteLine("  ToolsVersion="4.0">");
    writer.WriteLine("  <ItemGroup>");

    foreach (var item in Items)
    {
      string meta1 = item.GetMetadata("Meta1");
      string meta2 = item.GetMetadata("Meta2");
      writer.WriteLine("    <CopyItem Include="{0}">", item.ItemSpec);
      writer.WriteLine("      <Meta1>{0}</Meta1>", meta1);
      writer.WriteLine("      <Meta2>{0}</Meta2>", meta2);
      writer.WriteLine("    </CopyItem>");
    }
    writer.WriteLine("  </ItemGroup>");    
    writer.WriteLine("</Project>");    
  }
  ]]>
    </Code>
  </Task>
</UsingTask>

<ItemGroup>
  <OriginalItem Include="A">
    <Meta1>A1</Meta1>
    <Meta2>A2</Meta2>
  </OriginalItem>
  <OriginalItem Include="B">
    <Meta1>B1</Meta1>
    <Meta2>B2</Meta2>
  </OriginalItem>
</ItemGroup>
<Target Name="WriteItemsWithMetadata">
  <WriteItemsWithMetadata
    OutputFile="Out.props"
    Items="@(OriginalItem)"
    />
  <Exec Command="type Out.props" />
</Target>

这篇关于将项目传递给 MSBuild 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆