给定一个构建文件(.csproj或msbuild.xml或任何其他文件),我想运行一个msbuild命令来列出所有可用的、已定义的目标。
有这样的功能吗?
我知道我可以在构建文件上执行Xpath搜索,但这不会找到在包含文件中定义的目标。
给定一个构建文件(.csproj或msbuild.xml或任何其他文件),我想运行一个msbuild命令来列出所有可用的、已定义的目标。
有这样的功能吗?
我知道我可以在构建文件上执行Xpath搜索,但这不会找到在包含文件中定义的目标。
您可以像这样编写自定义的 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中,您可以使用新的闪亮物品:内联任务。 内联任务允许您直接在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>
从Visual Studio 16.6 preview 1开始,此功能已经在MSBuild中实现。
示例用法:
msbuild myProject.proj -targets
这应该会随着Visual Studio 16.6 preview 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
- Denis535Dotnet Core 6更新
dotnet "C:\Program Files\dotnet\sdk\6.0.300\MSBuild.dll" HelloWorld.csproj -targets
或者
dotnet msbuild HelloWorld.csproj -targets