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

5

有没有一种方法可以在每次构建结束时始终运行一个目标?

我知道我可以像这样做...

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

...但这样做不够规范,因为我需要为每个需要拆卸的目标编写一个包装器。

有什么想法吗?

谢谢, Roy

4个回答

4

使用构建监听器,例如 exec-listener ,它为每个构建结果提供任务容器(BUILD SUCCESSFUL | BUILD FAILED),您可以在其中放置所有需要的任务。
有关详细信息,请参见Ant 中针对 exec 失败的条件任务或自行创建构建监听器,参见 http://ant.apache.org/manual/develop.html 和您的 Ant 安装的 API 文档 = $ANT_HOME/docs/manual/api/org/apache/tools/ant/BuildListener.html。


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>

拦截其他事件应该很容易,正如您所见。

如果 script 在一个 target 内部,那么 buildStartedbuildFinished 事件无法成功触发。可以通过将 script 直接放置在 project 下来解决。 - Alex

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属性,但是在构建开始时这样做会错过很多(如果它们作为目标的一部分初始化,则在将它们转储到文件后)。
不幸的是,我不能分享具体的代码,但这只是一个实现了SubBuildListener的Ant任务,特别是buildFinished(BuildEvent)。监听器测试嵌入式条件,然后如果为true,则执行嵌入式sequential。
我很想知道是否有更好的方法。

0
如果这是一个Android项目,你可以轻松地在本地的“build.xml”文件中覆盖“-post-build”目标。该目标仅用于此目的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接