更新时间:2023-01-11 16:37:12
您需要使用 finalizedBy
以及特定任务的状态检查。以下是如何完成的:
You need to utilize finalizedBy
along with particular task's state checking. Here's how it can be done:
task connectedAndroidTest << {
logger.lifecycle("Running $name")
if (project.hasProperty('lol')) {
throw new Exception('lol')
}
}
task taskX << {
def failure = tasks.connectedAndroidTest.state.failure
if(!failure) {
logger.lifecycle("$name is finalizer")
} else {
logger.lifecycle("$tasks.connectedAndroidTest.name failed, nothing to do.")
}
}
connectedAndroidTest.finalizedBy(taskX)
现在如果运行:
Now if run with:
gradle cAT
输出结果为:
the output will be:
:connectedAndroidTest
Running connectedAndroidTest
:taskX
taskX is finalizer
BUILD SUCCESSFUL
Total time: 1.889 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.8/userguide/gradle_daemon.html
何时:
When:
gradle cAT -Plol
运行,则输出为:
is run, then the output is:
:connectedAndroidTest
Running connectedAndroidTest
:connectedAndroidTest FAILED
:taskX
connectedAndroidTest failed, nothing to do.
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/opal/tutorial/***/34797260/build.gradle' line: 4
* What went wrong:
Execution failed for task ':connectedAndroidTest'.
> lol
* 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: 1.931 secs
这里可以找到演示。
[1]: https://docs.gradle.org/current/javadoc/org/gradle/api/Task.html#finalizedBy(java.lang.Object...)