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.
="1.0" ="utf-8"
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build">
-->
<PropertyGroup>
<BuildDir>C:\Users\Amiralles\Documents\Articles\MSBuildFS\MSBuild_from_scratch
</BuildDir>
<ProgramsFilesDir>C:\Program Files</ProgramsFilesDir>
</PropertyGroup>
-->
<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