John Lam 1997 — 2026

Fun with NAnt

I decided to take some time out on my flight home from Tech-Ed in New Orleans to work on the build system for my Cross-Language Aspect Weaver (CLAW).[1] After watching lots of discussion about the merits of a one-step build, deploy and test script, I decided to bite the bullet and start taking a closer look at the NAnt (.NET Ant) build tool

NAnt is a very powerful build tool. It maps XML elements in a build script to tasks. Tasks perform various operations that are essential to any build process, such as running compilers, copying and deleting files, running unit tests, and even sending email to team members informing them of the status of that build.

NAnt is more-or-less a port of the Java Ant build tool. However, it is adapted to specifically meet the needs of CLR developers. For example, where the Ant tool has a task for running the javac compiler, NAnt has a task for running the csc compiler. There are also small, arbitrary, differences in standard tasks such as the echo task; the message attribute is mandatory in NAnt and optional in Ant.

Here’s a simple NAnt build script:

<project name="Hello NAnt" default="debug"> <target name="debug"> <echo message="DEBUG build"/> <csc target="exe" output="HelloWorld.exe" debug="true"> <sources> <includes name="HelloWorld.cs"/> </sources> </csc> </target> </project>

The script performs the equivalent of typing:

csc /target:exe /debug+ HelloWorld.cs

on the command line.

To invoke this script, you only have to type nant on the command line. NAnt will automatically search for the first file (in alphabetical order) in the current directory that contains a .build extension.

Now, what if you wanted to create multiple targets? Here’s a simple script that will do either a debug or a release build.

<project name="Hello Nant" default="debug">

Note that there two different output targets in this build script. To select the appropriate target, you need to indicate the target on the nant command line; nant release generates a release build, and nant debug generates a debug build.

In this build script, you’ll find that both the release and debug builds build to the same directory. Unfortunately, this means that you can’t switch between a debug and a release build without deleting the output file first, otherwise you’re risking the build not happening because the target is up to date. One possible solution to this problem is to have separate debug and release directories, much like what Visual C++ and C# do. In a future entry in this log, I’ll outline what I’ve come up with to solve this problem.

[1] I decided to code-name my AOP project CLAW. Guenter Kniesel came up with this clever acronym to describe my upcoming talk at the Aspect Oriented Software Development conference.