从VisualStudio运行自定义msbuild目标 [英] run a custom msbuild target from VisualStudio

查看:96
本文介绍了从VisualStudio运行自定义msbuild目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我向csproj文件添加了自定义目标.有没有办法从Visual Studio中运行该目标?我不希望它成为构建前或构建后的步骤,我只是想能够从Visual Studio中运行此目标(及其依赖项).

Suppose I add a custom target to a csproj file. Is there a way to run that target from visual studio? I don't want it make it a prebuild or postbuild step, I just want to be able to run this target (and its dependencies) from visual studio.

推荐答案

使用自定义外部工具有一种简单的方法(尽管不是很令人满意).

There is a simple way (though not all that satisfying) using a custom external tool.

假设您的项目文件进行了以下修改:

Assuming your project file has the following modification:

  <Target Name="CalledFromIde">
    <Error Text="Called from the IDE!" />
  </Target>

转到工具|外部工具并添加这样的内容:

Go to Tools | External Tools and add one like this:

  Title:      Called from IDE
  Command:    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe
  Arguments:  $(ProjectDir)$(ProjectFileName) /t:CalledFromIde
  Initial directory:  $(ProjectDir)
  Use Output window:  checked

运行此命令将产生如下输出:

Running this produces output as:

  Build FAILED.

  "F:\Code\CsProject\CsProject.csproj" (CalledFromIde target) (1) ->
  (CalledFromIde target) -> 
    F:\Code\CsProject\CsProject.csproj(57,5): error : Called from the IDE!

您正在做的是调用MSBuild作为外部工具,并使其直接运行目标.您必须提供MSBuild的完整路径,因为IDE不会维护与其创建的构建环境可用的属性相同的属性.

What you are doing is calling out to MSBuild as an external tool and having it run the target directly. You have to supply the full path to MSBuild because the IDE doesn't maintain the same properties that the build environment it creates has available.

您可以通过找出工具集中的哪个命令#来将其连接到快捷方式.

You can hook this up to a shortcut by figuring out which command # it is in the set Tools.ExternalCommand#.

如果您正在寻找一种更加复杂的解决方案,那就需要更多的精力.简而言之(对于VS2010):

If you're looking for a solution with more sophistication, it is a bit more involved. Here it is in a nutshell (for VS2010):

1)创建VS加载项(文件|新建|项目|其他项目类型|可扩展性| Visual Studio加载项).我不确定是否需要安装VS SDK才能在扩展管理器中找到它.

1) Create a VS Addin (File | New | Project | Other Project Types | Extensibility | Visual Studio Add-in). I'm not sure if you have to have the VS SDK installed to get this, it is available in the extension manager.

在向导中选择以下选项: -Microsoft Visual Studio 2010 -是的,创建一个工具"菜单项 -在应用程序启动时加载 -我的加载项永远不会建立模式UI,并且可以与命令行构建一起使用.

Select the following options in the wizard: - Microsoft Visual Studio 2010 - Yes, create a 'Tools' menu item - Load when the Application starts - My Add-in will never put up modal UI, and can be used with command line builds.

2)添加对Microsoft.Build和Microsoft.Build.Framework的引用

2) Add references to Microsoft.Build and Microsoft.Build.Framework

3)在Connect.cs文件中找到Exec的实现

3) Find the implementation of Exec in the Connect.cs file

4)替换为以下代码:

4) Replace it with this code:

public void Exec(
    string commandName,
    vsCommandExecOption executeOption,
    ref object varIn,
    ref object varOut,
    ref bool handled)
{
    handled = false;
    if (executeOption != vsCommandExecOption.vsCommandExecOptionDoDefault)
        return;
    if (commandName != "BuildAddin.Connect.BuildAddin")
        return;

    var doc = _applicationObject.ActiveDocument;
    var projectItem = doc.ProjectItem;
    var project = projectItem.ContainingProject;
    var evalProject =
        Microsoft.Build.Evaluation.ProjectCollection
        .GlobalProjectCollection.LoadProject(project.FullName);
    var execProject = evalProject.CreateProjectInstance();

    bool success = execProject.Build("CalledFromIde", null);

    var window = _applicationObject.Windows.Item(Constants.vsWindowKindOutput);
    var output = (OutputWindow)window.Object;
    OutputWindowPane pane = output.OutputWindowPanes.Add("BuildAddin");
    pane.OutputString(success ? "built /t:CalledFromIde" : "build failed");

    handled = true;
    return;
}

5)调试时有更好的自定义目标,因为前一个错误:

5) A better custom target while debugging, since the previous one errors:

  <Target Name="CalledFromIde">
    <WriteLinesToFile File="CalledFromIde.txt" Lines="Called from the IDE!" />
  </Target>

6)上面的代码没有为简洁起见进行错误检查,您将要更加简洁,因为它将在IDE中运行.该插件将在工具"菜单上放置一个菜单项.如上所述,它只是在寻找包含当前活动的编辑器文档的项目,因此无论您要做什么,都需要更好的选择.

6) The code above has no error checking for brevity, you'll want to be much cleaner since it will be running in the IDE. The addin will place a menu item on your Tools menu. As written above, it simply looks for the project containing the currently active editor document, which would need some better plumbing for whatever you are cooking up.

该技术从IDE内部获取构建引擎实例,并使其在项目的单独实例上执行构建.

This technique gets the build engine instance from within the IDE and has it execute a build on a separate instance of the project.

这篇关于从VisualStudio运行自定义msbuild目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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