ant - 在每个构建结束时运行目标

时间:2011-07-14 15:56:13

标签: ant

有没有办法在每个构建的 end 处始终运行目标?

我知道我可以做这样的事情......

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

...但这很草率,因为我需要一个需要拆卸的每个目标的包装器。

有什么想法吗?

谢谢, 罗伊

4 个答案:

答案 0 :(得分:4)

使用buildlistener,f.e。 exec-listener为每个构建结果提供一个任务容器
(BUILD SUCCESSFUL | BUILD FAILED),您可以将所有需要的任务放在中 有关详细信息,请参阅Conditional Task on exec failure in Ant 或者滚动你自己的构建监听器,参见=
http://ant.apache.org/manual/develop.html
和你的蚂蚁装置的api文件=
$ ANT_HOME /文档/手动/ API /组织/阿帕奇/工具/蚂蚁/ BuildListener.html

答案 1 :(得分:1)

与Rebse的答案一样,您可以在Javascript中编写BuildListener,如下所示(如果您不介意针对makefile中的某些代码交易Ant扩展):

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

如您所见,拦截其他事件应该非常简单。

答案 2 :(得分:0)

我有类似的需求,最后编写了一个自定义任务,如下所示:

<!-- 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>

在这种情况下,我想要一个对构建做出贡献的所有Ant属性的记录,但是在构建开始时这样做会错过很多(如果它们在转储后被初始化为目标的一部分)到文件)。

不幸的是,我无法共享特定代码,但这仅仅是实现Task的Ant SubBuildListener,特别是buildFinished(BuildEvent)。监听器测试嵌入条件,然后,如果true,则执行嵌入式sequential

我很想知道是否有更好的方式。

答案 3 :(得分:0)

如果是Android项目,您可以轻松覆盖本地“build.xml”文件中的“ -post-build ”目标。这个目标只是为了这个目的。