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="${foo.report.log}"/>
 <record name="${foo.report.log}" action="start"/>
 <!-- ant target that prints out info, but should fail, goes here -->
 <record name="${foo.report.log}" action="stop"/>
 <loadfile srcFile="${foo.report.log}" property='foo.report.content'/>
 <propertyregex property="foo.report.failed"
                input="${foo.report.content}"
                regexp=".*Exception: (.*)"  <!-- You should tweak this exception -->
                select="\1"/>
 <fail if="foo.report.failed"
       message="foo failed: ${foo.report.failed}"/>

2 comments:

  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?

    ReplyDelete
  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!

    ReplyDelete