NAnt exec error handling




If you are executing an external program in your NAnt script and want to have a bit more control over when a task is considered a success or failure, this little config snippet should help.

In this article I assume you are already familiar with NAnt.  If it is new to you, check it out here.

First thing you need to do is add the 'failonerror="false"' attribute to the exec node.  This will ensure that the task won't fail if the external program fails (pretty self explanatory actually)...

Next you need to capture the value returned from the program being executed.  This is done with the 'resultproperty='<propname>'.

Finally, you can then specify the conditions of failure.  This is acheived with the '<fail if='<condition>'>MESSAGE</fail> node.

The folowing example shows the execution of 'nunit console' in my NAnt build script.

<target name="tests">
          <exec basedir="..\..\Tools\NUnit"
                    useruntimeengine="true"
                    workingdir="${builddirectory}"
                    program="nunit-console.exe"
                    commandline="UnitTests.dll /xml=UnitTests-Result.xml"
                    failonerror="false"
                    resultproperty="nunitReturnCode" />

          <fail if="${nunitReturnCode == '1'}">Build failed (return code 1)</fail>    
</target>


This will ensure the "tests" task will only fail if a result of '1' is returned from nunit-console.  This way you can refine what you consider to be a failure, rather than using the default fail conditions.



 del.icio.us  Stumbleupon  Technorati  Digg 

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this entry.
Comments

Leave a comment

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.