java 如果任务失败,如何执行 Ant 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1017456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:49:09  来源:igfitidea点击:

How do I execute an Ant command if a task fails?

javaant

提问by tomjen

Suppose I have some Ant task - say javac or junit - if either task fails, I want to execute a task, but if they succeed I don't.

假设我有一些 Ant 任务——比如 javac 或 junit——如果其中一个任务失败,我想执行一个任务,但如果他们成功,我不会。

Any idea how to do this?

知道如何做到这一点吗?

回答by CoverosGene

In your junittarget, for example, you can set the failureProperty:

例如,在您的junit目标中,您可以设置failureProperty

<target name="junit" depends="compile-tests" description="Runs JUnit tests">
    <mkdir dir="${junit.report}"/>
    <junit printsummary="true" failureProperty="test.failed">
        <classpath refid="test.classpath"/>
        <formatter type="xml"/>
        <test name="${test.class}" todir="${junit.report}" if="test.class"/>
        <batchtest fork="true" todir="${junit.report}" unless="test.class">
            <fileset dir="${test.src.dir}">
                <include name="**/*Test.java"/>
                <exclude name="**/AllTests.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

Then, create a target that only runs if the test.failedproperty is set, but fails at the end:

然后,创建一个仅在test.failed设置属性时运行但最终失败的目标:

<target name="otherStuff" if="test.failed">
    <echo message="I'm here. Now what?"/>
    <fail message="JUnit test or tests failed."/>
</target>

Finally, tie them together:

最后,将它们联系在一起:

<target name="test" depends="junit,otherStuff"/>

Then just call the testtarget to run your JUnit tests. The junittarget will run. If it fails (failure or error) the test.failedproperty will be set, and the body of the otherStufftarget will execute.

然后只需调用test目标来运行您的 JUnit 测试。该junit目标将运行。如果失败(失败或错误),则该test.failed属性将被设置,并且otherStuff目标的主体将执行。

The javactask supports failonerrorand errorPropertyattributes, which can be used to get similar behavior.

javac的任务支持failonerrorerrorProperty属性,可以用来获得类似的行为。

回答by bebbo

as mentioned from Kai:

正如凯所说:

ant-contribhas a trycatch task.

ant-contrib有一个 trycatch 任务。

But you need the recent version 1.0b3. And then use

但是您需要最新的 1.0b3 版本。然后使用

<trycatch>
    <try>
        ... <!-- your executions which may fail -->
    </try>
    <catch>
        ... <!-- execute on failure -->
        <throw message="xy failed" />
    </catch>
</trycatch>

The trick is to throw an error again to indicate a broken build.

诀窍是再次抛出错误以指示构建损坏。

回答by Kai Huppmann

ant-contribhas a trycatch task.

ant-contrib有一个 trycatch 任务。

回答by jrharshath

Set a property in the task for which you want to check for failure, and then write the second task so that it executes if the property is not set. I don't remember the exact syntaxes for build.xml, or I'd give examples.

在要检查失败的任务中设置一个属性,然后编写第二个任务,以便在未设置该属性时执行。我不记得 build.xml 的确切语法,或者我会给出示例。