Is there an onExit hook in gradle?


#1

I need to call a method to report success/failure to a local web service when gradle finishes, with different options for success and fail of the gradle build.

Is there any mechanism available in gradle for this?

I know I can do it with a wrapper script, but that’s ugly because some of the status info that’s reported is only available in gradle and I’d have to pass back a lot of state into.


(René Groeschke) #2

Hello Thag,

you can register a buildListener on the gradle object or just use the gradle.buildFinished hook:

gradle.buildFinished{result ->
   ...
//your custom code
...
  }

have a look at the DSL reference for details: http://www.gradle.org/docs/current/dsl/org.gradle.api.invocation.Gradle.html#org.gradle.api.invocation.Gradle:buildFinished(groovy.lang.Closure)

cheers, René


#3

Thanks Rene, that is exactly what I needed. Works perfectly.

For the record, the result can be checked for errors and acted on accordingly:

gradle.buildFinished{result ->
  if(result.getFailure() == null) {
  sendNotification(completionUri, 'COMPLETED', completionData)
 }
 else {
  println "Failure code: ${result.getFailure()}"
  sendNotification(completionUri, 'FAILED')
 }
}