|Home||Back to Index|
We wanted to run some NAnt tasks and detect failures without stopping the build. This isn’t as easy as it sounds.
The first trick is to put the tasks to run in a separate build file. You run this with an nant task with failonerror=false.
In the child build file, use the nant.onfailure property to define a target to run on failure.
The problem is notifying the parent process of the child’s failure. Initially I tried to set a property of the parent; this does not work. Properties are passed by value. Apparently there was a proposal for a byref option at one point, but it never went anywhere.
I found that environment variables work for this purpose. Place a setenv task in the child build file under the onfailure target. Check this value via a environment::get-variable function call in the parent build file after the call to the child. You will also want another setenv task in the parent build file before the call to the child, as get-variable expects the variable to exist.
This isn’t as intuitive as most NAnt tasks, but it allows you to set up fairly sophisticated error handling. In our case, we wanted the script to build a number of projects during the nightly build and to not fail fast (build subsequent projects if a project failed). However, if any project failed the nightly build script should fail. This allows us to capture all the build results in CruiseControl’s logs.