开发者

ant - Running a target at the end of every build

Is there a way to always run a target at the end of every build?

I know I can do something like this...

<target name="runJob" depends="actuallyRunJob, teardown"/>

... but that's sloppy, since I'd need a wrapper for every target that needs a teardown.

Any ideas?

Tha开发者_StackOverflow社区nks, Roy


use a buildlistener, f.e. the exec-listener which provides a taskcontainer for each build result
( BUILD SUCCESSFUL | BUILD FAILED ) where you can put all your needed tasks in
see Conditional Task on exec failure in Ant for further details
or roll your own build listener, see =
http://ant.apache.org/manual/develop.html
and the api docs from your ant installation =
$ANT_HOME/docs/manual/api/org/apache/tools/ant/BuildListener.html


In the same spirit as Rebse's answer, you can write a BuildListener in Javascript as follows (if you don't mind trading an Ant extension against some code in your makefile):

<project name="test-ant" default="build">                                                           

<target name="init">                                                                            
    <script language="javascript"> <![CDATA[                                                    
        function noop () {}                                                                     
        var listener = new org.apache.tools.ant.BuildListener() {                               
            buildFinished: function(e) {                                                        
                project.executeTarget("_finally");                                              
            },                                                                                  
            buildStarted: noop,                                                                 
            messageLogged: noop,                                                                
            targetStarted: noop,                                                                
            targetFinished: noop,                                                               
            taskStarted: noop,                                                                  
            taskFinished: noop                                                                  
        }                                                                                       
        project.addBuildListener(listener)                                                      
    ]]></script>                                                                                
</target>                                                                                       

<target name="build" depends="init"/>                                                           

<target name="fail" depends="init">                                                             
    <fail message="expected failure"/>                                                          
</target>                                                                                       

<target name="_finally">                                                                        
    <echo message="executing _finally target..."/>                                              
</target>                                                                                       

</project>

Intercepting other events should be quite easy as you can see.


I had a similar need and ended up writing a custom task, which looks like this:

<!-- a task that executes the embedded sequential element at the end of the build,
    but only if the nested condition is true; in this case, we're dumping out the
    properties to a file but only if the target directory exists (otherwise, the clean
    target would also generate the file and we'd never really be clean!) -->
<build:whendone>
    <condition>
        <available file="${project_target-dir}" />
    </condition>
    <sequential>
        <mkdir dir="${project_final-build-properties-file}/.." />
        <echoproperties destfile="${project_final-build-properties-file}" />
    </sequential>
</build:whendone>

In this case, I wanted a record of all of the Ant properties that contributed to a build, but doing that at the beginning of the build misses quite a few (if they're initialized as part of targets, after dumping them to the file).

Unfortunately, I can't share specific code, but this is nothing more than an Ant Task that implements SubBuildListener, specifically buildFinished(BuildEvent). The listener tests the embedded condition and then, if true, executes the embedded sequential.

I'd love to know if there's a better way.


If it's an android project you can easily override the "-post-build" target in your local "build.xml" file. This target is just for this purpose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜