John Lam 1997 — 2026

Effective NAnt

I spent a lot more time than I had wanted to working on the build process for CLAW. In retrospect, I think that I had seriously underestimated the complexities involved in building a fully automated build script. Throw in the learning curve associated with writing NAnt scripts, and then adding a few features to NAnt that I thought were essential and you have all of the ingrediants for a blown schedule ;)

I have finally arrived at a build script that I think I’m happy with. Along the way, however, I made a whole bunch of observations about creating build files that I wanted to collect in one place. This is the purpose of this entry: to try and share a bit of what I learned.

Clean and prepare tasks should be separated

This is a total non-issue if you are building simple projects that consist entirely of one build file. In this case, combining both operations in the same task is no different than executing each of these tasks separately.

CLAW, however, is built from a series of separate projects. In this case, your clean and prepare tasks must be separated; particularly if you’re concerned about maintaining encapsulation in your build files. This is necessary because the clean task is a destructive task, and the prepare task is a constructive task. The tasks need to call nested tasks in reverse order.

The clean task must tell all child tasks to clean before it does its own cleaning. This must be done because your child projects will most likely be managing directories underneath the directory that is managed by the master project. You cannot delete a directory that still contains other files / directories; you must clean the child directories before the parent directories.

Similarly, your prepare task must build directories beginning with the master project’s directories, followed by the child project’s directories.

Beware of tri-state builds

In NAnt, it’s very straightforward to execute either a release build or a debug build by writing tasks that examine the state of a property called, for example, debug. The targets would look like:

` … …

`

If debug is true, only the compile-debug target will execute. Similarly, if debug is false, only the compile-release target will execute.

But what should you do if you need to build both debug and release builds? You might be tempted to create a new target called compile-all. However, if you do this you will wind up with an explosion in the number of targets that you must define. For example, you cannot simply modify a property on the fly within a method; the following code will NOT work:

<target name="compile"> <property name="debug" value="true"/> <call target="compile-debug"/> <property name="debug" value="false"/> <call target="compile-release"/> </target>

This is due to the fact that properties are not evaluated during the execution of a task. They are evaluated prior to executing the code within a task.

While there are certainly solutions to this problem, (I’ll leave it as an exercise to the reader to figure out the solution), that solution is NOT ideal. Instead, what you should do is define a second property called release. Your code now becomes:

` … …

`

Both targets will compile if the debug and release properties are both set to true.

Ensure that your unit tests succeed before checking in your build

There is really no point checking in your build if your unit tests fail on that build. Therefore, your unit tests must return a non-zero result if they fail. For example if you were using a CppUnit test harness, your main method should resemble:

`int _tmain(int argc, _TCHAR* argv[]) { CppUnit::TextUi::TestRunner runner; runner.addTest( ComTest::suite() );

return runner.run() ? 0: 1; }`

A non-zero return value will cause an exception to fire inside of NAnt. When this happens you must take appropriate action.

To handle exceptions in NAnt, you must define a property called nant.onfailure and set its value to the name of the target that you want to execute in event of failure. In the case of my build file, I point nant.onfailure to a target that will undo all prior SourceSafe checkout operations.

2 archived reader comments

Saeid (2003-04-30)

Would you please send me all of your knowledge about NAnt? We are at desing step for a PointOfSale project. thanks in advance

**** (2003-10-23)

I agree with Saeid… could you just serialize your brain and email it to me? Binary serialization preferrable. Thanks in advance.