I have a gradle taskX
that I would like to run after connectedAndroidTest
task finishes, however only if all tests pass in connectedAndroidTest
.
How I can achieve this?
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/stackoverflow/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
Here a demo can be found.
[1]: https://docs.gradle.org/current/javadoc/org/gradle/api/Task.html#finalizedBy(java.lang.Object...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With