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

查看:57
本文介绍了将项目传递给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天全站免登陆