Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run a custom msbuild target from VisualStudio

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.

like image 218
Frank Schwieterman Avatar asked Feb 26 '11 02:02

Frank Schwieterman


People also ask

How do I run MSBuild target?

To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.

Will MSBuild compile without any target?

If MSBuild determines that any output files are out of date with respect to the corresponding input file or files, then MSBuild executes the target. Otherwise, MSBuild skips the target. After the target is executed or skipped, any other target that lists it in an AfterTargets attribute is run.

How do I run MSBuild in Visual Studio?

To install MSBuild on a system that doesn't have Visual Studio, go to Build Tools for Visual Studio 2019, or install the . NET SDK. If you have Visual Studio, then you already have MSBuild installed. With Visual Studio 2022, it's installed under the Visual Studio installation folder.

Does Visual Studio call MSBuild?

Visual Studio uses MSBuild, but MSBuild doesn't depend on Visual Studio. By invoking msbuild.exe on your project or solution file, you can orchestrate and build products in environments where Visual Studio isn't installed.


2 Answers

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! 

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#.

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) 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.

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) Add references to Microsoft.Build and Microsoft.Build.Framework

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

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) 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) 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.

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

like image 73
Brian Kretzler Avatar answered Sep 20 '22 13:09

Brian Kretzler


If you are running the build inside of Visual Studio there will be a build variable of VisualStudioDir during the build.

To execute only is a VS build session do this:

<Target Name="Test" BeforeTargets="Build" Condition="'$(VisualStudioDir)' != ''> </Target> 

To execute only in a build outside of VS do this:

<Target Name="Test" BeforeTargets="Build" Condition="'$(VisualStudioDir)' == ''> </Target> 

You will need to include your custom targets file in one of two ways.

  1. Set the environment variable CustomBeforeMicrosoftCommonTargets
  2. Edit you project file to include your custom targets file by adding an import

    <Imports Project="CustomBuildTasks.Targets"><Imports/> 
like image 28
Tim Schruben Avatar answered Sep 19 '22 13:09

Tim Schruben