Ant中exec失败的条件任务 [英] Conditional Task on exec failure in Ant

查看:40
本文介绍了Ant中exec失败的条件任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些通过 Ant 运行的单元测试,如果单元测试失败,我希望能够运行一些清理代码.我正在寻找某种最终"块,但我没有找到一个.我试过在任务上使用 errorproperty 和 if 语句,但 ant 只接受true"、on"和yes"作为真正的属性.成功执行的任务(至少在 unix 上)返回 0,所以我不得不构建一个极其复杂的装置:

I have some unit tests running through Ant, and I'd like to be able to run some cleanup code if the unit tests fail. I was looking for some sort of "finally" block, but I had no luck finding one. I've tried using errorproperty and if statements on tasks, but ant only accepts "true", "on" and "yes" as true properties. A successfully executed task (on unix at least) returns 0, so I've had to construct a ridiculously elaborate apparatus:

<project name="TestBuild" default="build" basedir=".">
<target name="build" depends="truth,checkresult,cleanup" />

<target name="truth">
    <echo message="Running Truth" />
    <exec executable="false" errorproperty="testfailure"/>
</target>
<target name="checkresult">
    <condition property="testfailed">
        <not>
            <equals arg1="${testfailure}" arg2="0" />
        </not>
    </condition>
</target>
<target name="cleanup" if="testfailed">
    <echo message="cleanup" />
    <fail />
</target>

有没有更简单的方法来做到这一点?一方面,这需要完成任务,这看起来很荒谬.这也意味着我必须在每个单元测试块之后都调用它们,因为我显然无法像往常一样设置 failonerror.总而言之,这是一个笨拙、不雅的解决方案,我希望有人有更好的解决方案.

Is there any simpler way to do this? For one, this requires to tasks to complete, which seems ridiculous. It also means I'd have to call both of them after every block of unit tests, because I obviously can't set failonerror as I normally would. In all, it's a hacky, inelegant solution, and I'm hoping someone has better one.

推荐答案

两种可能性

-1-
对脚本的特定部分使用一些 try/catch/finally 构造
您需要一些提供这些功能的 Ant 插件,例如=

Flaka
Antcontrib/羚羊

    <trycatch>
     <try>
      <exec .../>
     </try>
     <catch>
      do your cleanup here
      and afterwards don't forget to fail
      </fail message="......."/>
     </catch>
      optionally you may use a finally section also
     <finally>
      ..
     </finally>
   </trycatch>

-2-
对整个脚本使用构建侦听器(构建成功,构建失败)

-2-
use a buildlistener for the whole script ( BUILD SUCCESSFUL, BUILD FAILED )

Kev Jackson 在他的演讲中有一个很好的 exec-listener 例子,= http://people.apache.org/~kevj/ossummit/extending-ant.html(幻灯片中包含 exec-listener 的来源)

Kev Jackson has a nice example of an exec-listener in his presentation, = http://people.apache.org/~kevj/ossummit/extending-ant.html (the sources of the exec-listener are included in the slides)

您可以在构建完成后根据构建结果启动特定任务

You're able to kick off specific tasks depending on the build result after your build has finished

<!-- taskcontainer -->    
<exec-listener onSuccess="true|false">
..

 your stuff goes here 
..
</exec-listener>

这篇关于Ant中exec失败的条件任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆