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.
相关问题
- How to know full paths to DLL's from .csproj f
- Importing NuGet references through a local project
- Visual Studio 2019 - error MSB8020: The build tool
- 'System.Threading.ThreadAbortException' in
- VS2017 RC - The following error occurred when tryi
相关文章
- How to show location of errors, references to memb
- How to track MongoDB requests from a console appli
- Visual Studio Hangs on Loading UI Library
- How to use Mercurial from Visual Studio 2010?
- Build errors of missing packages in Visual Studio
- Copy different file to output directory for releas
- Edit & Continue doesn't work
- “Csc.exe” exited with code -1073741819
You don't have to code with the Exec, although that is one way to do it. The easier way is to do the following:
Change the
DefaultTargets="Build"
attribute to a custom Target you create, say"All"
like so:Then in your custom "All" target, you can use the DependsOnTargets attribute, like the following:
This will then build, and out put zip files in your custom
"All"
target.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:
To execute only in a build outside of VS do this:
You will need to include your custom targets file in one of two ways.
Edit you project file to include your custom targets file by adding an import
There is a simple way (though not all that satisfying) using a custom external tool.
Assuming your project file has the following modification:
Go to Tools | External Tools and add one like this:
Running this produces output as:
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:
5) A better custom target while debugging, since the previous one errors:
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.
Some time ago I had the same problem and decided to write VS add-in. Try it:
https://github.com/Serg046/MsBuildTaskExplorer
https://marketplace.visualstudio.com/items?itemName=saaseev.MsBuildTaskExplorer