gpt4 book ai didi

msbuild - 有没有办法列出构建文件中可用的所有构建目标?

转载 作者:行者123 更新时间:2023-12-02 08:11:21 25 4
gpt4 key购买 nike

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

这个功能存在吗?

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

最佳答案

使用 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,您可以使用新的 Shiny 东西:内联任务。内联任务允许您直接在 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>

关于msbuild - 有没有办法列出构建文件中可用的所有构建目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2618201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com