我创建了一个抛出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
时,sampleA
和sampleB
都会运行。虽然我希望sampleA
失败,但不应该sampleB
我错过了什么?
答案 0 :(得分:2)
StopExecutionException
停止执行当前任务,并继续执行下一个任务(请参阅Javadoc)。要停止构建,抛出任何其他异常(GradleException
是一种常见的选择)。