Raihan Iqbal
Life is codetastic!

Discovering MSBuild – Part One

Background

This series is all about me trying to learn the dark art of MSBuild. Ever since I started developing .NET applications, I was aware of the existence of this strange tool but I could never harness its true potential. So lets begin our adventure!

Introduction

An MSBuild file typically known as an MSBuild Project File contains instructions on how to build your application step by step – neat stuff! This innovative tool from Microsoft helps you to take control over every aspect of the build process thereby helping you to automate the build and deployment process.

Overview

An MSBuild project file is basically an XML file. This XML document is defined by two XSD documents Microsoft.Build.CommonTypes.xsd and Microsoft.Buid.Core.xsd. Both these files are located in the %WINDIR%\Microsoft.NET\Framework\vXXXX\MSBuild folder. The basic structure of an MSBuild project file looks like this:

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

This structure is comprised of Properties, Items, Targets. Confused? You should be. ;) Put it simply, items are the files that are going to be built and properties are the build parameters ex. Configuration or OutputPath.

Project Properties

Properties are nothing but Key-Value pairs and apparently there two different types of Properties – Static, Dynamic. What I know about static properties is that they must be contained within a PropertyGroup element which is declared directly under the project element. A project element may contain multiple PropertyGroup elements.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <ApplicationName>MyDemoApp</ApplicationName>
        <WhyAmICrazy>I Have No Idea</WhyAmICrazy>
    </PropertyGroup>
</Project>

In the above example, ApplicationName is the key and MyDemoApp is the value(DUH!!). Can values contain spaces? I have no idea – so lets find out. Paste the above code in notepad and save it with a *.proj extension, fire up the Visual Studio Command Prompt, and execute the script.

Noname

Oppsss! Look’s like we are missing something called Targets? Not to worry, let’s dig deeper.

Tasks & Targets

Targets are the work horses of the MSBuild file so without them the file would be literally useless. Properties are utilized within these targets. A task is the smallest unit of work and a target is a sequential set of tasks. The MSBuild tool ships with a predefined set of tasks like Copy, Csc(wow haven’t used this baby in a while!), Move, Exec, etc. Tasks are invoked using parameters which are just XML attributes with values. The attributes may vary from task to task. For our example we will try out the Message task.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <ApplicationName>MyDemoApp</ApplicationName>
        <WhyAmICrazy>I Have No Idea</WhyAmICrazy>
    </PropertyGroup>
    <Target Name="HelloWorld">
        <Message Text="This is my first MSBuild Application" />
    </Target>
</Project>

To build this project the command should be of the format

msbuild FILE_NAME /t:TARGET_NAME

For our example the command would look like

msbuild Sample1.proj /t:HelloWorld

You will see that the build succeeds without any errors! We may have won the battle but not the war ;)

A more complicated example for you adrenaline junkies out there:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <ApplicationName>MyDemoApp</ApplicationName>
        <WhyAmICrazy>I Have No Idea</WhyAmICrazy>
    </PropertyGroup>
    <Target Name="HelloWorld">
        <Message Text="This is my first MSBuild Application and $(WhyAmICrazy) why I love .NET" />
    </Target>
</Project>

 

Until the next episode..

Share

Posted in .net, msbuild, Tools | 3,508 views | 1 Comment

Tags: , ,

One Response to “Discovering MSBuild – Part One”

  1. Rajesh says:

    Cool explanation…Absolutely cleared the basics and made me dig deeper into MSBUILD which earlier used to appear boring to read about…Good job mate…Keep it up!!!

Leave a Reply