Click here to Skip to main content
Licence CPOL
First Posted 13 May 2009
Views 9,399
Downloads 91
Bookmarked 17 times

MSBuild from Scratch

By | 13 May 2009 | Article
An article on how to create a build file for MSBuild

Introduction

In this article, I'll try to show you how to create a MSBuild script file from scratch. The idea behind the code is to provide a very simple, but working script, to automate a build process.
Build engines provide a wide range of amazing features that can make our life ease, but the problem with them is the fact that you have to deal with script files, although once you get familiar with them, is not so hard to handle them, when you are new, you may get lost trying to find out a starting point.

Based on my experience, there's lot of resources on the web, for developers who have some background or have worked with similar tools like NAnt, but if you're new in the neighbourhood, you may spend a lot of time reading the scripts and trying to understand what's going on.
So I decided to create a pretty straightforward script, keeping the code as simple as possible, that handles the following tasks:

  • Builds a solution
  • Runs the unit tests
  • Analyzes the code
  • Creates backup files
  • Creates the installer file (.msi)

Just for the sake of simplicity, I decided not to include fancy features of MSBuild and work around the very basic and most commonly used ones.

Using the Code

Before taking a look at the code, I want to mention all of the programs that you need installed on your machine to make it work properly.
Well, obviously MSBuild, but you also need to install:

  • NUnit 2.4.8
  • Microsoft FxCop 1.35
  • WinRAR
  • Visual Studio 2008

If you don't want to install any of these programs or you can't get them, you just need to comment the line that executes them in the script below.

Within the download files, I include a Visual Studio solution, that contains some C# projects and a unit test suite. I made this to let you focus just on the build process.

Note: As a final advice, I strongly recommend that you check the path of all files used in the script. Sometimes for instance, when you install another version of NUnit, the installation folder ain't the same that you have configured in the script and then the build process fails, not because the source code has got errors or because of failure of the unit tests suite, but just because of wrong paths in the script.

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

    <!--Within the PropertyGroup section, you can define all 
        of the variables that you need for your build script.
        In this case, I just use just two, one to specify the 
        build directory and the other to 
        specify the programs files directory.
        To make this build script work properly, you just need to 
        replace the paths below to match with your computer configuration.-->
    <PropertyGroup>
      <BuildDir>C:\Users\Amiralles\Documents\Articles\MSBuildFS\MSBuild_from_scratch
	</BuildDir>
      <ProgramsFilesDir>C:\Program Files</ProgramsFilesDir>
    </PropertyGroup>
    
    <!--Clean and Build the solution.-->
    <MSBuild> 
      Projects="$(BuildDir)\Src\MSBuildFromScratch.sln"
      Properties="Configuration=Debug"
      Targets="Clean;Build"
      ContinueOnError="false"
      StopOnFirstFailure="true"/>

Conclusion

Well, in conclusion I would say that sometimes, taking your time to understand build engines and script files can save you several hours along the project development life cycle.
Any comments or suggestions about the article will be appreciated. Please contact me at info@amiralles.com.ar.

History

  • 13th May, 2009: Initial post

License

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

About the Author

Alejandro Miralles

Architect
FPA Software
Argentina Argentina

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[My vote of 2] need much more PinmemberDonsw6:51 13 Jun '09  
QuestionWhere is doc form classes msbuild? Pinmemberjuliotrujilloleon20:44 19 May '09  
AnswerRe: Where is doc form classes msbuild? PinmemberAlejandro Miralles6:54 20 May '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 13 May 2009
Article Copyright 2009 by Alejandro Miralles
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid