I have a basic ant script in which I copy a set of files into a directory outside of any target. I would then like to clean those files up after any/all targets have run regardless of dependencies. The main problem I'm having is that the target can be 'compile' or 'deploywar' so I can't just blindly call the 'cleanUp' target from 'compile' because 'deploywar' might get called next. And I can't blindly call from just 'deploywar' because it might not get called. How can I define a target that will get called after all other necessary targets have been completed (either failed or successful)? The 'cleanUpLib' target below is the target I would like to have called after all/any tasks have executed:
<project name="proto" basedir=".." default="deploywar">
...
<copy todir="${web.dir}/WEB-INF/lib">
<fileset dir="${web.dir}/WEB-INF/lib/common"/>
</copy>
<target name="compile">
<!-- Uses ${web.dir}/WEB-INF/lib -->
....
</target>
<target name="clean" description="Clean output directories">
<!-- Does not use ${web.dir}/WEB-INF/lib -->
....
</target>
<target name="deploywar" depends="compile">
<!-- Uses ${web.dir}/WEB-INF/lib -->
....
</target>
<target name="cleanUpLib">
<!-- Clean up temporary lib files. -->
<delete>
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
</delete>
</target>
The build listener solution pointed to by Rebse looks useful (+1).
An alternative you could consider would be to "overload" your targets, something like this:
You can't really overload in a single Ant build file of course, so the target names must be different.
(I've used the "-" prefix above which is a hack to make targets "private" - i.e. you can't invoke them from the command line due to shell script arg processing. But of course you could still double-click them successfully in Ant).
To run a target after any/all targets regardless of dependencies you may use a build listener or some try/catch/finally pattern, for details see: