Gradle如何在任务失败后继续构建

如果我们运行Gradle构建并且其中一项任务失败,则整个构建将立即停止。因此,我们可以快速反馈构建状态。如果我们不想这样做,并且希望Gradle执行所有任务,即使某些任务可能失败了,我们也可以使用命令行选项--continue。当我们使用--continue命令行选项时,Gradle将执行从属任务没有失败的所有任务。这在多模块项目中也很有用,即使在某些项目中测试可能失败,我们也可能希望构建所有项目,因此我们可以全面了解所有模块的失败测试。

在下面的Gradle构建文件中,我们有两个任务。任务failTask抛出TaskExecutionException故意使任务失败。该successTask不会失败:

task failTask << { task ->
    println "Running ${task.name}"
 
    throw new TaskExecutionException(
            task, 
            new Exception('Fail task on purpose')) 
}
 
task successTask << {
    println "Running ${it.name}"
}

让我们从命令行运行这两个任务并查看输出:

$ gradle failTask successTask
:failTask
Running failTask
:failTask FAILED
 
FAILURE: Build failed with an exception.
 
* Where:
Build file '/Users/mrhaki/samples/gradle/continue/build.gradle' line: 4
 
* What went wrong:
Execution failed for task ':failTask'.
> Fail task on purpose
 
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
 
BUILD FAILED
 
Total time: 4.148 secs
$

我们看到构建失败,仅failTask执行任务。现在我们运行相同的两个任务,但是我们使用命令行选项--continue

$ gradle --continue failTask successTask
:failTask
Running failTask
:failTask FAILED
:successTask
Running successTask
 
FAILURE: Build failed with an exception.
 
* Where:
Build file '/Users/mrhaki/samples/gradle/continue/build.gradle' line: 4
 
* What went wrong:
Execution failed for task ':failTask'.
> Fail task on purpose
 
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
 
BUILD FAILED
 
Total time: 6.918 secs
$

这次,successTask即使failTask再次失败,也会执行。Gradle将跟踪所有失败的任务,并显示所有失败任务的摘要。

技术类文章精选

非技术文章精选

原文地址:https://www.cnblogs.com/FunTester/p/11926397.html