【问题标题】:Is there a way to list all the build targets available in a build file?有没有办法列出构建文件中可用的所有构建目标?
【发布时间】:2010-04-11 18:34:17
【问题描述】:

给定一个构建文件(.csproj 或 msbuild.xml 或其他),我想运行一个 msbuild 命令来列出所有可用的已定义目标。

这个功能存在吗?

我知道我可以在构建文件上执行 Xpath 搜索或其他操作,但找不到包含文件中定义的目标。

【问题讨论】:

  • 关闭!我真的很想从 msbuild 本身做。我想答案是否定的。
  • 你可以在 PowerShell 脚本中执行上述代码。
  • 是的,没错。编写脚本不会很困难。

标签: msbuild


【解决方案1】:

使用 MSBuild 2.0/3.5:自定义任务

您可以像这样编写自定义 msbuild 任务:

using System;
using System.Collections.Generic;
using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace MSBuildTasks
{
  public class GetAllTargets : Task
  {
    [Required]
    public String ProjectFile { get; set; }

    [Output]
    public ITaskItem[] Targets { get; set; }

    public override bool Execute()
    {
      var project = new Project(BuildEngine as Engine);
      project.Load(ProjectFile);

      var taskItems = new List<ITaskItem>(project.Targets.Count);
      foreach (Target target in project.Targets)
      {
        var metadata = new Dictionary<string, string>
                        {
                          {"Condition", target.Condition},
                          {"Inputs", target.Inputs},
                          {"Outputs", target.Outputs},
                          {"DependsOnTargets", target.DependsOnTargets}
                        };
        taskItems.Add(new TaskItem(target.Name, metadata));
      }

      Targets = taskItems.ToArray();

      return true;
    }
  }
}

你会这样使用:

<Target Name="TestGetAllTargets">
  <GetAllTargets ProjectFile="$(MSBuildProjectFile)">
    <Output ItemName="TargetItems" TaskParameter="Targets"/>
  </GetAllTargets>

  <Message Text="Name: %(TargetItems.Identity) Input: %(TargetItems.Input) --> Output: %(TargetItems.Output)"/>
</Target>

使用 MSBuild 4.0:内联任务

使用 MSBuild 4,您可以使用新的闪亮的东西:内联任务。内联任务允许您直接在 msbuild 文件中定义行为。

<UsingTask TaskName="GetAllTargets"
           TaskFactory="CodeTaskFactory"
           AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
  <ParameterGroup>
    <ProjectFile ParameterType="System.String" Required="true"/>
    <TargetsOut ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true"/>
  </ParameterGroup>
  <Task>
    <Reference Include="System.Xml"/>
    <Reference Include="Microsoft.Build"/>
    <Reference Include="Microsoft.Build.Framework"/>
    <Using Namespace="Microsoft.Build.Evaluation"/>
    <Using Namespace="Microsoft.Build.Execution"/>
    <Using Namespace="Microsoft.Build.Utilities"/>
    <Using Namespace="Microsoft.Build.Framework"/>
    <Code Type="Fragment" Language="cs">
      <![CDATA[
        var project = new Project(ProjectFile);
        
        var taskItems = new List<ITaskItem>(project.Targets.Count);
        foreach (KeyValuePair<string, ProjectTargetInstance> kvp in project.Targets)
        {
          var target = kvp.Value;
          var metadata = new Dictionary<string, string>
                          {
                            {"Condition", target.Condition},
                            {"Inputs", target.Inputs},
                            {"Outputs", target.Outputs},
                            {"DependsOnTargets", target.DependsOnTargets}
                          };
          taskItems.Add(new TaskItem(kvp.Key, metadata));
        }

        TargetsOut = taskItems.ToArray();
      ]]>
    </Code>
  </Task>
</UsingTask>

<Target Name="Test">
  <GetAllTargets ProjectFile="$(MSBuildProjectFile)">
    <Output ItemName="TargetItems" TaskParameter="TargetsOut"/>  
  </GetAllTargets>

  <Message Text="%(TargetItems.Identity)"/>
</Target>

【讨论】:

    【解决方案2】:

    从 Visual Studio 16.6 预览版 1 开始,这是在 MSBuild 中实现的。

    示例用法: msbuild myProject.proj -targets

    这应该会在 Visual Studio 16.6 预览版 1 中推出(在未来的某个时间点)。

    (来源:https://github.com/microsoft/msbuild/pull/5032#issuecomment-587901124

    文档:

    -targets[:file]
                     Prints a list of available targets without executing the
                     actual build process. By default the output is written to
                     the console window. If the path to an output file
                     is provided that will be used instead.
                     (Short form: -ts)
                     Example:
                       -ts:out.txt
    

    【讨论】:

    • 要使用它,你应该打开终端并写下一个:msbuild Solution\Project.csproj -targets
    • 有没有办法列出任务?
    猜你喜欢
    • 1970-01-01
    • 2022-10-03
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    相关资源
    最近更新 更多