Reputation: 20129
Given the "build.gradle" file below I want to make sure that stopServer
is run when I call testServerLifecycle
. Currently if the runTestOnServer
task fails the build stops.
If runTestOnServer
is the only thing on the task graph I do not want to run stopServer
.
build.gradle
tast startServer {}
task runTestOnServer {
dependsOn startServer
}
tast stopServer {}
task testServerLifecycle {
dependsOn runTestOnServer
finalizedBy stopServer
}
Is this type of functionality supported by Gradle? What can I do ensure that stopServer
is run when testServerLifecycle
is on the task graph?
Details: Gradle 1.9
gradle.taskGraph.afterTask { Task task, TaskState state ->
if (task == runTestOnServer
&& state.failure
&& gradle.taskGraph.getAllTasks().contains(stopServer)) {
println "----------------- Stopping Server -----------------"
tasks.stopServer.execute()
}
}
I was not able to use finalizedBy
on the 'runTestOnServer' task, because I wanted to run 'stopServer' if and only if 'stopServer' would have run normaly; using finalizedBy
would have forced it to run every time.
It should be noted that this gets run after every task.
Upvotes: 1
Views: 1455
Reputation: 4584
Since finalizedBy
is not transitive across tasks, it should be applied to the startServer
task directly. I think something like this should cover it:
task startServer { // ...}
task stopServer { // ...}
startServer.finalizedBy('stopServer')
stopServer.mustRunAfter('runTestOnServer')
task runTestOnServer {
dependsOn startServer
// ...
}
Alternatively, you could use afterTask
and remove both stopServer and testServerLifecycle
gradle.taskGraph.afterTask { Task task, TaskState state ->
if (task == runTestOnServer) {
// clean up, implement stopServer here
}
}
You can also make different rules based on the state of the task like this:
gradle.taskGraph.afterTask { Task task, TaskState state ->
if (state.failure) {
// Handle the failure
println "$task FAILED!!!"
} else {
println "SUCCESS!!!"
}
}
This should be run after every task
is executed.
Upvotes: 2