MSBuild: By Example—Integrating MSBuild into Visual Studio

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
Visual Studio Articles

MSBuild: By Example

© 2006 Hashimi & Hashimi

Integrating MSBuild into Visual Studio

Throughout this chapter you have been using MSBuild at the command line. We have a few reasons for giving these instructions: to emphasize the separation of MSBuild from Visual Studio and because complex builds are more easily executed from the command line. However, you can integrate MSBuild into Visual Studio in a simple way. In Visual Studio, you can add menu items to the Tools menu to invoke any executable. This is the method you will employ to integrate MSBuild into Visual Studio. To do this, follow these simple steps:

1. Open the External Tools dialog box by selecting Tools -> External Tools.

v2. Add a new tool by clicking Add.</p>

3. Enter MSBuild (or anything you’d like to call it) for the title.

4. For the command, provide the full path to the msbuild.exe file.

5. For the arguments, you need to provide the full path to the project file. First add the project directory, as shown in Figure 3-8.



//Figure 3-8. MSBuild project directory specification


6. Then, add a backward slash (\) followed by the project filename. Specify the project filename in a similar way as you specified the project directory.

7. Following this, you can specify any arguments for MSBuild in the same text box.

8. Set Initial Directory to be Project Directory, just like in Figure 3-8.

9. Also check the Use Output Window and Prompt for Arguments boxes.

10. The resultant dialog box should look similar to Figure 3-9. Click OK to finish.


Image:6528f0309.jpg
Figure 3-9. MSBuild integration into Visual Studio


As you can see in Figure 3-9, the verbosity of MSBuild has been specified to be detailed (d), and a target has also been specified. Since you have checked the Prompt for Arguments radio button, you can change these default arguments for each execution of MSBuild if you choose.

After you close the dialog box, make sure one of your project files is open. Invoke MSBuild by selecting Tools -> MSBuild (or whatever you set as the value for the title). At this point, you will see a dialog box similar to Figure 3-10.


Image:6528f0310.jpg
Figure 3-10. MSBuild dialog box in Visual Studio


As mentioned, this specifies a default verbosity and the target that you want to have executed, but you can change this before every invocation if you choose. Also, if you’d like to change the default values, use the External Tools dialog box to modify the MSBuild external tool definition. As you specify these values, keep in mind that this will be the same for every instance of Visual Studio. Because of this, many times it is inappropriate to specify a default target, unless it is a common one such as Build. After you have decided what arguments to specify, click the OK button, and your build will begin. Since you checked the Use Output Window box, all of the output from MSBuild will be redirected to the Output window inside Visual Studio. After executing this task, you will see the Output window shown in Figure 3-11.


Note This view of the Output window shows only the end of the MSBuild output.




Figure 3-11. Output window from MSBuild execution inside Visual Studio


As we have shown, it is simple to integrate MSBuild into Visual Studio by using the External Tools dialog box in Visual Studio. This is particularly useful for targets that you find yourself frequently executing.


Previous_Page_.gif Next_Page_.gif

Personal tools