Wednesday, September 16, 2009

Ant Hack: From Log Message To Exception

I was annoyed that a particular Ant target I was using was logging its exception, rather than throwing it. This results in a useful error message printed to the screen, yet the task succeeds as though all went just fine.

Here's a work around I put together to capture and analyze the output and turn it into a real failure. Note - other than using some tags from the Ant contrib package, this is all standard stuff.

The basic strategy I used was to capture the task output, read it into a property and then analyze it.

 <delete file="${}"/>
 <record name="${}" action="start"/>
 <!-- ant target that prints out info, but should fail, goes here -->
 <record name="${}" action="stop"/>
 <loadfile srcFile="${}" property=''/>
 <propertyregex property=""
                regexp=".*Exception: (.*)"  <!-- You should tweak this exception -->
 <fail if=""
       message="foo failed: ${}"/>


  1. I am sorry you had to do something like that.

    Did you submit a patch to them where the exception is just thrown instead?

  2. You know I haven't. My line of thinking was that they were happy with what they had developed. But, perhaps I should drop them a line and tell them my logic.

    Thanks Grant!