Ant 中执行失败时的条件任务

发布于 2024-11-15 17:10:13 字数 961 浏览 2 评论 0原文

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

<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。总而言之,这是一种老套、不优雅的解决方案,我希望有人有更好的解决方案。

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>

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹梦 2024-11-22 17:10:13

两种可能性

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

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-
对整个脚本使用构建侦听器(构建成功,构建失败)

Kev Jackson 在他的演示文稿中提供了一个很好的 exec-listener 示例,= http://people.apache.org/~kevj/ossummit/extending-ant.html (来源幻灯片中包含了 exec-listener 的内容)

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

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

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

Two possiblities

-1-
use some try/catch/finally construct for specific parts of your script
you need some Ant Plugin that provides those features, f.e. =

Flaka
Antcontrib / Antelope

    <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-
use a buildlistener for the whole script ( BUILD SUCCESSFUL, BUILD FAILED )

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>
捎一片雪花 2024-11-22 17:10:13

Ant contrib 有 try-catch-finally 的概念。然而,这是针对特定块的finally,而不是针对整个脚本的。

Ant contrib has the concept of a try-catch-finally. However this is a finally for a particular block, not for the entire script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文