Building .NET Applications with NAnt

Although Visual Studio .NET does a number of things very well to assist in the development process, there are certainly areas that need improvement. One of those areas is dealing with a build process that requires something a little more than just compiling source code.

by Dave Bost

Jun 15, 2005

Page 1 of 4

key tool in any software development arsenal is a structured build process. NAnt is a tool for building Visual Studio .NET applications that you can incorporate into your development process and that provides something greater than simply compiling code.

In teams of developers, there is always going to be a need for a structured build process. This can be a manual process, but most likely you'll want to automate this process as much as possible. In today's ultra-fast world, continuous integration is key to determining where you are in your development project. If multiple developers check in code at various times, it is always a good thing to have an "integration check"something to make sure all the code gels together.

The idea behind continuous integration is that when a developer checks code in, a process is triggered to perform a system build to make sure the code doesn't break the build. This action usually involves the execution of unit tests as well. If the build, along with the unit tests, succeeds, the developer has immediate feedback that the check-in didn't break the build. No one wants to be the person who broke the build!

The typical build process involves actions like these:

Pulling source updates from the source code repository

Preparing the build directories

Compiling the source code

Running the unit tests

Creating the setup packages

Deploying the output binaries

Notifying the team that a new build is available

If you incorporated Visual Studio .NET into a build process such as this, you would create an elaborate batch file that calls out to Visual Studio .NET's command-line (devenv.exe) along with the additional tools to handle the tasks that .NET couldn't. Another way to attack it may be to write some hooks into the .NET IDE automation model to handle some of these tasks during a build, but who has the time? The good news is that there are tools available built specifically to handle these types of build process requirements. One of the more popular build tools in the .NET community is NAnt.

NAnt
NAnt is an open-source build automation tool that works off of an XML document to execute a sequence of defined tasks that build .NET applications. NAnt is a port of the popular Ant build tool from the Java community. Ant was created to address some of the deficiencies of older shell-based build tools such as make and nmake. If you've ever written make scripts, you will appreciate NAnt's ease of use and its extensible architecture.

NAnt works well as an automated build tool. It supports compiling C#, Visual Basic .NET, C++, and J# files. It can compile these files against the .NET Framework platform [v1.x, V2.0 (Beta 1)] and Mono.

Installing NAnt
The first thing to incorporate NAnt into your build process is to get NAnt up and running on your computer. You can download the latest stable release or a recent nightly build from the project site. The nightly builds are typically very stable, but for the purposes of this article, I will work with the latest stable release at the time of this writing, NAnt v.0.85-rc1.

Once you've downloaded the zip package, unzip the contents into a directory on your file system. To make it easier for you to execute your NAnt build scripts, place the location of NAnt's \bin directory into your system's PATH environment variable. You can test your installation by opening a command shell and typing nant -help at the command prompt. If everything is successful, you will see a list of command-line options for the NAnt build tool.

The NAnt Build Script
Executing the build script is as simple as typing in "nant"at the command prompt. NAnt probes its current directory (the directory from which it was executed) for any file ending in .build, and attempts to execute it. If more than one file within the directory ends in .build, NAnt throws an exception unless one of the files is named default.build. You can also inform NAnt which script you wish to execute by specifying the -f flag:

C:> nant -f:MyApp.build

The NAnt build script is an XML document comprised of one <project> tag, and zero to many <target> tags with each <target> containing a set of tasks to execute. To make it a little easier to digest the terminology, you can think of a <target> as a method call and a task as a command.

Each element type in a NAnt build script (the project, target, tasks, etc.) has a defined set of attributes associated with that particular type. Some of these attributes are required and others are optional. These attributes provide additional details about how to execute each particular type.

To better understand the build script, it is wise to get a better understanding of the main parts of any build script.