Understanding Microsoft Build System – What is MSBuild

MSBuild is perhaps one of the most used but uncredited piece of technology. The Microsoft Build Engine or more known as MSBuild, is a platform for building applications. Chances are that if you have ever used Visual Studio or compiled a .NET based project, you have used it knowingly or un-knowingly. Visual Studio uses MSBuild, but it 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 is not installed. For MSBuild to work properly, you need to use an XML schema that defines how the build platform processes and builds software.

In this blog post, we’ll learn just basics of MSBuild, understanding what it is. Through a series of upcoming posts, we’ll learn how to use the XML schema so that MSBuild can build software as per our needs.

As we discussed above, MSBuild uses an XML-based project file format that is straightforward and extensible. The MSBuild project file format lets developers describe the items that are to be built, and also how they are to be built for different operating systems and configurations. In addition, the project file format lets developers author reusable build rules that can be factored into separate files so that builds can be performed consistently across different projects in the product.

Role of Visual Studio

Visual Studio normally creates and manages the project files (.csproj,.vbproj, vcxproj, and others) containing MSBuild XML code that executes when you build a project by using the IDE. Visual Studio projects import all the necessary settings and build processes to do typical development work, but you can extend or modify them from within Visual Studio or by using an XML editor. So most of the time, you would not notice that you are using it. However, it is not required to use visual studio to build source code. You can run MSBuild on a machine which does not have Visual Studio installed fine.

Download MSBuild

Earlier, it used to be installed with .NET framework. However, starting from Visual Studio 2013, you can find MSBuild.exe in the MSBuild folder (%ProgramFiles%\MSBuild on a 32-bit operating system, or %ProgramFiles(x86)%\MSBuild on a 64-bit operating system).

Alternatively, if you have Visual Studio installed, you can use the Visual Studio Command Prompt, which has a path that includes the MSBuild folder.

The latest (as of mid-2017) stand-alone MSBuild installers can be found here: https://www.visualstudio.com/downloads/

Scroll down to “Other Tools and Frameworks” and choose “Build Tools for Visual Studio 2017” (despite the name, it’s for users who don’t want the full IDE).

Alternatively, you can google for “Microsoft Build Tools” and download the latest version available.

Define a basic project file

Since at very basic, it requires a project file to work, you need to pass it to the MSBuild. The simplest MSBuild project file would contain the following XML data:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

Above XML code will identify that this is an MSBuild project file. Everything else needs to be placed inside the Project element. In terms of MSBuild XML schema, you would need to define properties, items, targets, etc. When building software applications, you will always need to know two pieces of information: what is being built and what build parameters are being used.

Running MSBuild at command prompt

Once we have created a project file, we can use msbuild at command line along with certain options. Command-line options let you set properties, execute specific targets, and set other options that control the build process. For example, below command tells MSBuild to build MyProj project in Debug configuration mode:

MSBuild.exe MyProj.proj /property:Configuration=Debug

Configure SYSTEM PATH

If you want to be able to run MSBuild from any directory, you need to add MSBuild path to environment variable PATH.

For this, at the command prompt, type set PATH=%PATH%;%ProgramFiles%\MSBuild or set PATH=%PATH%;%ProgramFiles(x86)%\MSBuild depending upon where it is installed.

You can also do the same using GUI. Otherwise, you need to navigate to the directory containing MSBuild and then run it from there.

One thought on “Understanding Microsoft Build System – What is MSBuild

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s