问题描述
我有一个基本的 ant 脚本,我在其中将一组文件复制到任何目标之外的目录中.然后我想在任何/所有目标都运行后清理这些文件,而不管依赖关系.我遇到的主要问题是目标可以是compile"或deploywar",所以我不能盲目地从compile"中调用cleanUp"目标,因为接下来可能会调用deploywar".而且我不能盲目地从部署战"中调用,因为它可能不会被调用.如何定义在所有其他必要目标完成(失败或成功)后将被调用的目标?下面的cleanUpLib"目标是我想在所有/任何任务执行后调用的目标:
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>
推荐答案
Rebse 指出的构建侦听器解决方案看起来很有用 (+1).
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:
<project default="compile">
<target name="compile" depends="-compile, cleanUpLib"
description="compile and cleanup"/>
<target name="-compile">
<!--
your original compile target
-->
</target>
<target name="deploywar" depends="-deploywar, cleanUpLib"
description="deploywar and cleanup"/>
<target name="-deploywar">
<!--
your original deploywar target
-->
</target>
<target name="cleanUpLib">
</target>
</project>
当然,您不能在单个 Ant 构建文件中真正重载,因此目标名称必须不同.
You can't really overload in a single Ant build file of course, so the target names must be different.
(我使用了上面的-"前缀,这是一种使目标成为私有"的黑客 - 即,由于 shell 脚本 arg 处理,您无法从命令行调用它们.但当然,您仍然可以加倍- 在 Ant 中成功点击它们).
(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).
这篇关于无论依赖项如何,如何强制执行最终的 ant 目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!