简体   繁体   English

无条件地在ant中执行任务?

[英]Unconditionally execute a task in 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. 我正在尝试定义在目标完成执行时发出(使用echo)消息的任务,无论该目标是否成功。 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? 我正在尝试做什么?

Update: It turns out I'm dumb. 更新:事实证明我是愚蠢的。 I had haltOnFailure="true" in my <testng> task, which explains the behaviour I was seeing. 我的<testng>任务中有haltOnFailure =“true”,这解释了我所看到的行为。 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. 现在的问题是,将此设置为false会导致整个ant构建成功,即使测试失败,这也不是我想要的。 The answer below using the task looks like it might be what I want.. 以下使用该任务的答案看起来可能是我想要的......

According to the Ant docs , there are two properties that control whether the build process is stopped or not if the testng task fails: 根据Ant文档 ,如果testng任务失败,有两个属性可以控制构建过程是否停止:

haltonfailure - Stop the build process if a failure has occurred during the test run. haltonfailure - 如果在测试运行期间发生故障,则停止构建过程。 Defaults to false. 默认为false。

haltonskipped - Stop the build process if there is at least on skipped test. haltonskipped - 如果至少跳过测试,则停止构建过程。 Default to false. 默认为false。

I can't tell from the snippet if you're setting this property or not. 如果您正在设置此属性,我无法从片段中分辨出来。 May be worth trying to explicitly set haltonfailure to false if it's currently set to true. 如果当前设置为true,可能值得尝试将haltonfailure显式设置为false。

Also, assuming you're using the <exec> functionality in Ant, there are similar properties to control what happens if the executed command fails: 此外,假设您在Ant中使用<exec>功能,还有类似的属性来控制执行的命令失败时会发生什么:

failonerror - Stop the buildprocess if the command exits with a return code signaling failure. failonerror - 如果命令退出并返回代码信号失败,则停止构建过程。 Defaults to false. 默认为false。

failifexecutionfails - Stop the build if we can't start the program. failifexecutionfails - 如果我们无法启动程序,请停止构建。 Defaults to true. 默认为true。

Can't tell based on the partial code snippet in your post, but my guess is that the most likely culprit is failonerror or haltonfailure being set to true. 根据帖子中的部分代码片段无法分辨,但我的猜测是最可能的罪魁祸首是failonerrorhaltonfailure设置为true。

You can use a try-catch block like so: 您可以像这样使用try-catch块:

<target name="myTarget">
    <trycatch property="foo" reference="bar">
        <try>
            <testing outputdir="${results}" ...>
                ...
            </testing>
        </try>

        <catch>
            <echo>Test failed</echo>
        </catch>

        <finally>
            <echo>Tests complete.  Results available in ${results}</echo>
        </finally>
    </trycatch>
</target>

The solution to your problem is to use the failureProperty in conjunction with the haltOnFailure property of the testng task like this: 您的问题的解决方案是将failureProperty与testng任务的haltOnFailure属性一起使用,如下所示:

<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: 然后,在其他地方,当您希望构建失败时,添加如下所示的ant代码:

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

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

You can then call doSomethingIfTestsFailed where you want your ant build to fail. 然后,您可以在您希望您的ant构建失败的地方调用doSomethingIfTestsFailed。

Although you are showing a fake task called "testng" in your example I presume you are using the junit target. 虽然您在示例中显示了一个名为“testng”的假任务,但我认为您正在使用junit目标。

In this case, it is strange you are seeing these results because the junit target by default does NOT abort execution on a test failure. 在这种情况下,您看到这些结果很奇怪,因为默认情况下,junit目标不会在测试失败时中止执行。

There is a way to actually tell ant to stop the build on a junit failure or error by using the halt attributes, eg. 有一种方法可以通过使用暂停属性来实际告诉ant停止构建junit失败或错误,例如。 haltonfailure : haltonfailure

<target name="junit" depends="junitcompile">
    <junit printsummary="withOutAndErr" fork="yes" haltonfailure="yes">

However, both haltonfailure and haltonerror are by default set to off. 但是, haltonfailurehaltonerror都默认设置为off。 I suppose you could check your build file to see if either of these flags have been set. 我想你可以检查你的构建文件,看看是否已经设置了这些标志。 They can even be set globally, so one thing you could try is to explicitly set it to "no" on your task to make sure it is overridden in case it is set in the global scope. 它们甚至可以全局设置,因此您可以尝试的一件事是在您的任务上明确地​​将其设置为“no”,以确保在全局范围内设置它时它被覆盖。

http://ant.apache.org/manual/Tasks/junit.html http://ant.apache.org/manual/Tasks/junit.html

Can you fork the testng task ? 你可以分叉testng任务吗? If yes, then, you might want to use that feature so that the testng task will run on a different JVM. 如果是,那么,您可能希望使用该功能,以便testng任务将在不同的JVM上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM