如何继续执行任务不依赖于失败的dependsOn任务

时间:2014-08-04 09:43:13

标签: gradle build.gradle

我创建了一个抛出StopExecutionException的自定义任务。但是,任务没有失败,gradle继续执行下一个任务,这取决于任务失败。

自定义任务:

class EMTest extends DefaultTask { {
    @TaskAction
    public void exec() {
        def outFile = new File(System.env.T_WORK + '/' + project.lrgName + '-' + name + '-gradle.out')
        def errFile = new File(System.env.T_WORK + '/' + project.lrgName + '-' + name + '-gradle.err')
        def execResult = this.executeTestNgTests(project.configurations.lrgConfig, testngXml, outputDir, outFile, errFile)
        try {
            execResult.assertNormalExitValue()
            if (errFile.length()) {

                println "Block ${name} failed. Throwing exception 2"
                throw new StopExecutionException("Block ${name} failed.")
            }
        } catch (ExecException e) {
            println "Block ${name} failed. Throwing exception"
            throw new StopExecutionException("Block ${name} failed.")
        }
    }
    protected ExecResult executeTestNgTests(def cp, testngXml, outputDir, outFile, errFile) {
        log.info("Classpath is " +  cp)
        def execResult = project.javaexec {
            ignoreExitValue true
            main "org.testng.TestNG"
            classpath cp
            args testngXml, "-d", outputDir

            standardOutput new FileOutputStream(outFile)
            errorOutput new FileOutputStream(errFile)
        }
        return execResult
    }
}

的build.gradle

task sampleA(type: EMTest)

task sampleB (type: EMTest, dependsOn: [sampleA])

现在,当我执行build.gradle时,sampleAsampleB都会运行。虽然我希望sampleA失败,但不应该sampleB

我错过了什么?

1 个答案:

答案 0 :(得分:2)

StopExecutionException停止执行当前任务,并继续执行下一个任务(请参阅Javadoc)。要停止构建,抛出任何其他异常(GradleException是一种常见的选择)。