How to force a final ant target to execute regardl

2019-06-23 19:22发布

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>

标签: ant target
2条回答
相关推荐>>
2楼-- · 2019-06-23 19:58

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>

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).

查看更多
聊天终结者
3楼-- · 2019-06-23 20:03

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:

查看更多
登录 后发表回答