Gradle task to run during error builds

Is there a way to make sure a specific task gets executed if the build fails, am looking for the nant ‘nant.onfailure’ equivalent in gradle.

I need to clean up the build location and store some build meta data at the end of the build and that specific task needs to be executed last, whether the build fails or succeeds.

The below isn’t exactly what you are asking for, since it isn’t a task, but it will allow you to determine the success/failure of a complete build and handle things accordingly. Examine the BuildListener interface and BuildResult class to see if they meet your needs.

gradle.addBuildListener(new BuildAdapter() {
    void buildFinished(BuildResult result) {
      if (result.failure) {
        // Handle error conditions
        println "Exception was thrown"
        println result.failure
      else {
        // Do normal stuff
        println "No problems"


thanks Spencer, this gives me at least some round about way but it would be nice if there is a way to call a specific task that is part of my custom plugin, so i dont have to duplicate the functionality in the plugin task, in the build finished closure as well Kal