Click here to Skip to main content
15,663,154 members
Articles / Programming Languages / XML
Posted 25 Sep 2012

Tagged as


89 bookmarked

Introduction to MSBuild - Part 1

Rate me:
Please Sign up or sign in to vote.
4.62/5 (34 votes)
30 Jun 2015CPOL6 min read
A step by step walk through of MSBuild file and how to run a target


Microsoft Build Engine (MSBuild) is the build platform for Microsoft and Visual Studio. The best part of MSBuild is its transparency as to how it processes the builds software which enables the developers to orchestrate the scripts as per the requirements.

Why do I need MSBuild?

Build during development and one time integration can easily be accomplished through IDE. And there is no better way of achieving it. But when it comes to automated build and continuous integration there needs to be a process in place that will replicate a specified number of tests and generate the output as per the requirements and standards set and then MS Build is the answer.

Getting to know the MSBuild

MS Build is an XML based file that is written in scripting language. The file in itself hosts a list of properties and functionalities that can be accessed via command line and a particular task can be automatically initiated without any manual intervention.

MS Build Structure Overview

For first those who are working with MS Build for the first time they will need to install MSBuild Tasks and Extension Packs. The software could be downloaded from –

The installation process is simple. Just download and run the installer. Once the installation is over, if the installation location has not been changed then at C:\ProgramFiles a new folder will be created with sub folders given as below:

 Image 1

Once the installation is done we can start creating our first MSBuild file.

A simple MSBuild file can be viewed without even writing a single line of code. Just follow the simple steps given below:

  • Open any existing visual studio solution.
  • Right click on any one of the projects under the solution (given in the image below)

 Image 2

  • <formulas /></formulas />
  • Select unload project.
  • Once the project is unloaded, right click on the project and select the edit project option like the image given below:

Image 3

  • A simple MSBuild files open for the particular project.

Once we go through the MSBuild file we may confidently conclude that a project file is nothing other than an MSBuild file that has set of instructions written in it.

The next thing is - How to read an MSBuild file?

Well what follows is the walkthrough of the various parts of the MSBuild file.

Our first glance through the file gives us some insight into what kind of file its is and our conclusions are as follows:

  • It’s a type of XML file
  • Each node is either a declaration or a property or an action.

Now let’s look deeper into the nodes.

  • First Node - The namespace declaration. The first line is a node of any MSBuild file looks something like the line given below.


<Project xmlns="" 
    DefaultTargets="Test" ToolsVersion="4.0">


In the above line there is couple of things that are new and few things that we have already used before. Let’s understand the attributes 1 by 1.

  • Project node – This represents the starting/root node in the file. This is a mandatory node. All the functionalities w.r.t a particular MSBuild task need to go within the project node. Also the references to all external resources to be used also need to go with in this node.
  • Xmlns attribute - This represents the XML namespace that will be referred to. In order to run MSBuild scripts we need to refer the particular namespace defined as ""
  • DefaultTargets attribute – This attribute refers to particular task that will run by default when no value is specified for the "/target:" switch from command line.
  • ToolsVersion attribute – This refers to the version of the Extension Pack Tools that we are using/is installed.
  • Declaration Nodes -Next within the "Project" node we declare the following:


<TPath Condition="Exists('$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks')">




<Import Project="$(TPath)"/>

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

The property group node encapsulates the various key value pairs that will be declared and used across the script for different targets. While the "PropertyGroup" node in itself is a system defined node the child nodes within it are user defined and is declared as per user requirement and has the following features:

  • The name of the child nodes could be any string with alphanumeric characters.
  • The key declared in the child nodes are valid as long as they are not repetitive, is a system defined keyword or has any special characters.
  • The values of the child nodes could be accessed via $(<childnode_name>) syntax.
  • In the above example there is a single "key" declared via child nodes within the "PropertyGroup" node. The child node in the example above has the following features
  • The declared key is "TPath"
  • Its value is -$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks
  • The "Condition" attribute to "TPath" checks whether the requisite files are available at the specified location/path.
    • Item Group node – This node contains a set of user-defined Item elements. Every item used in a MSBuild project must be specified as a child of an ItemGroup element. The item group can lists items of many types. Few common ones are -
      • Reference
      • Content
      • Compile
      • None
    • Imports node – This declares that the contents of another project file should inserted at the particular location where the imports node is found. A single MSBuild file may have multiple imports node.
    • Projects Extensions node – This is an optional section that is used by MSBuild hosts and may contain arbitrary XML content.
    • Target node – This is actually the node that encapsulates tasks for MSBuild to run sequentially. The common attributes of Target node are -
      • Name – This is the name of the target and is mandatory
      • DependsOnTarget – It’s optional and lists the targets that must be executed before a target can be executed or top-level dependency analysis can occur. Multiple targets are separated by semicolons.
      • Inputs - The item inputs for this target. Items in this attribute are used as the inputs in the top-level dependency analysis.
      • Outputs - The expected outputs of this target. You can apply transforms to the input items to generate the output items. For more information about transforms

Now since we are acquainted with structure of the MSBuild file, let’s try out few examples –

  1. Lets declare a section like the one given below in an MSBuild file–




<TPath Condition="Exists('$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks')">$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks 



<Import Project="$(TPath)"/> 

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/> 

<Target Name="WriteAMessage"> 

<Message Text="This is the test run"/> 

<Message Text="Hello World"/> 


  1. To run the above target follow the steps given below:
  • Click Start, then click All Programs. Locate and click the Visual Studio Command Prompt in the Visual Studio Tools folder.


  • From the command window, navigate to the folder containing the MSBuild file, in this case, D:\BuildApp\BuildApp.


  • Run msbuild with the command switch /t: WriteAMessage. This selects and builds the WriteAMessage target with command like this –


msbuild helloworld.msbuild /t: WriteAMessage

  • The output would be

Image 4

That was simple...we will continue with the other MSBuild functions in Part 2(can be found here).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionWill post an updated version soon. Pin
girlprogrammer7-May-17 20:27
girlprogrammer7-May-17 20:27 
QuestionWhat is the use of Condition attribute? Pin
cangaran 5-May-17 21:14
cangaran 5-May-17 21:14 
QuestionWhy does TPath Property element present two times? Pin
cangaran 5-May-17 20:04
cangaran 5-May-17 20:04 
SuggestionNeeds Updating Pin
David Boarman14-Mar-16 8:45
David Boarman14-Mar-16 8:45 
GeneralRe: Needs Updating Pin
girlprogrammer14-Mar-16 23:23
girlprogrammer14-Mar-16 23:23 
QuestionExpecting More in Part2 Pin
Mukesh.C.Gupta28-May-15 2:28
Mukesh.C.Gupta28-May-15 2:28 
AnswerRe: Expecting More in Part2 Pin
girlprogrammer29-Jun-15 21:28
girlprogrammer29-Jun-15 21:28 
GeneralGood Explanation Pin
Alireza_13627-May-15 17:33
Alireza_13627-May-15 17:33 
QuestionExcellent article. Pin
haughtycool2-Mar-15 15:39
haughtycool2-Mar-15 15:39 
AnswerRe: Excellent article. Pin
girlprogrammer29-Jun-15 21:28
girlprogrammer29-Jun-15 21:28 
GeneralMy vote of 3 Pin
VMAtm21-May-13 2:04
VMAtm21-May-13 2:04 
QuestionMy 5 Vote Pin
serhiy.semenyuk2-Oct-12 22:23
serhiy.semenyuk2-Oct-12 22:23 
GeneralMy vote of 5 Pin
fredatcodeproject2-Oct-12 3:04
professionalfredatcodeproject2-Oct-12 3:04 
GeneralMy vote of 5 Pin
David Pierson1-Oct-12 14:34
David Pierson1-Oct-12 14:34 
GeneralMy vote of 5 Pin
Wooters25-Sep-12 6:25
Wooters25-Sep-12 6:25 
GeneralRe: My vote of 5 Pin
girlprogrammer27-Sep-12 3:31
girlprogrammer27-Sep-12 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.