在ant中无条件执行任务? [英] Unconditionally execute a task in ant?

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

问题描述

我正在尝试定义一个任务,该任务在目标完成执行时发出(使用回显)消息,而不管该目标是否成功.具体来说,目标执行一个任务来运行一些单元测试,我想发出一条消息,指示结果可用的位置:

I'm trying to define a task that emits (using echo) a message when a target completes execution, regardless of whether that target was successful or not. Specifically, the target executes a task to run some unit tests, and I want to emit a message indicating where the results are available:

<target name="mytarget">
  <testng outputDir="${results}" ...>
    ...
  </testng>
  <echo>Tests complete.  Results available in ${results}</echo>
</target>

不幸的是,如果测试失败,任务将失败并中止执行.所以只有在测试通过时才会输出消息 - 与我想要的相反.我知道我可以把任务放在任务之前,但这会让用户更容易错过这条消息.我正在尝试做的事情可能吗?

Unfortunately, if the tests fail, the task fails and execution aborts. So the message is only output if the tests pass - the opposite of what I want. I know I can put the task before the task, but this will make it easier for users to miss this message. Is what I'm trying to do possible?

更新:事实证明我很笨.我的 <testng> 中有haltOnFailure="true"任务,这解释了我所看到的行为.现在的问题是,即使测试失败,将其设置为 false 也会导致整个 ant 构建成功,这不是我想要的.下面使用任务的答案看起来可能是我想要的..

Update: It turns out I'm dumb. I had haltOnFailure="true" in my <testng> task, which explains the behaviour I was seeing. Now the issue is that setting this to false causes the overall ant build to succeed even if tests fail, which is not what I want. The answer below using the task looks like it might be what I want..

推荐答案

解决您的问题的方法是将 failureProperty 与 testng 的 haltOnFailure 属性结合使用像这样的任务:

The solution to your problem is to use the failureProperty in conjunction with the haltOnFailure property of the testng task like this:

<target name="mytarget">
  <testng outputDir="${results}" failureProperty="tests.failed" haltOnFailure="false" ...>
    ...
  </testng>
  <echo>Tests complete.  Results available in ${results}</echo>
</target>

然后,当您希望构建失败时,您可以在其他地方添加如下蚂蚁代码:

Then, elsewhere when you want the build to fail you add ant code like this:

<target name="doSomethingIfTestsWereSuccessful" unless="tests.failed">
   ...
</target>

<target name="doSomethingIfTestsFailed" if="tests.failed">
   ...
   <fail message="Tests Failed" />
</target>

然后,您可以在希望 ant 构建失败的地方调用 doSomethingIfTestsFailed.

You can then call doSomethingIfTestsFailed where you want your ant build to fail.

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

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