Location: JWare Software » Log4Ant » Documents » How Tos » Emit Errors For All Targets
Emit Errors For All Targets
Q: How do I get errors emitted for all targets?
One common use for the <emit:show> task is to combine it with the AntXtras <protect> taskset to pass through both an error message, the associated exception information, and optionally, additional context information to the real logging system. The following snippet demonstrates how you might do this for a “main-libraries” target definition:
<target name="main-libraries"> <protect> ...[tasks that can fail]... <iferror capturethrown="last.error"> <emit:show messageid="E.something.failed" level="error"> <include thrown="last.error"/> </emit:show> </iferror> </protect> </target>
If this reaction is something you’d like done for many targets, you can leverage Ant’s built-in <presetdef> mechanism to declare a custom <protect> wrapper that you can reuse without repeating the same 7-8 lines of Ant script in multiple places. The presetdef (named uninterestingly as “wrap” here) would look like so:
[In your own antlib declare...] <presetdef name="wrap"> <protect> <iferror capturethrown="last.error"> <emit:show messageid="E.something.failed" level="error"> <include thrown="last.error"/> </emit:show> <unassign reference="last.error"/> </iferror> </protect> </presetdef>
Once you import the definition of the custom <wrap>, you can use it in as many targets as you need like so:
[Pull in your custom antlib...] <project ... xmlns:my="myantlib"> <taskdef resource="myantlib.xml" uri="myantlib"/> [Then reuse in many targets...] <target name="main-libraries"> <my:wrap> ...[tasks that can fail]... </my:wrap> </target> <target name="test-libraries"> <my:wrap> ...[tasks that can fail]... </my:wrap> </target> <target name="programmertests"> <my:wrap> ...[tasks that can fail]... </my:wrap> </target> ...