/ MSBuild

MSBuild Part 1: Say Hello

Ever wondered what happens when you hit Ctrl + Shift + B in Visual Studio? Or what lives in the project file? Well I did, so I decided to take a closer look. But all fairy tails has to start somewhere, and our starts with MSBuild.

This post is part of the following series:

What is MSBuild?

MSBuild is the build platform for Microsoft and Visual Studio. It is mainly used for building software and automating common tasks for developers. MSBuild is not only used by Visual Studio, it is also used by Team Foundation Server and it is also possible to run from the Command Line.

Enough of that, lets get to some code :-)

Hello MSBuild

Open up any text editor that you like, i will use Visual Studio Code.

Create a new file named construction.msbuild at the location C:\MSBuildDemo with the following content:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <Target Name="Foundation">

        <Message Text="Foundation for construction done" />



As you probably noticed it is just a XML file with some special nodes and attributes. So lets go through and explain how the file is constructed:

  • Project node - Required root node that is the entry point for all MSBuild instructions
  • xmlns attribute - Required namespace that resolves to an XML Schema Definition included in the .NET framework
  • Target node - A container for instructions with a required name
  • Message node - A MSBuild Task, almost like a function or external tool thats get invoked by MSBuild. In this case it is the Message Task, that writes a text message to the Command Line. For a list of default tasks go here.

To run your MSBuild script, open Visual Studio Command Prompt. If you need help to find the tool, just follow this guide.

Change the location to your C:\MSBuildDemo folder and type the following to invoke your script:

c:\MSBuildDemo>msbuild construction.msbuild 

MSBuild Run Script

Ta da! You have successfully run your first MSBuild Script :) Besides running our Message Task, MSBuild also provides some useful information about when the build started and the location of our script file. It also tells you if the build succeeded or if any warnings or errors occurred. Finally it also provides information about the time it took to execute the script.

This will conclude our first experience with MSBuild and our first step to understand what is behind the mysterious Ctrl + Shift + B in Visual Studio.

Next time we will dig deeper and focus on Targets, see you next time!