Is there anything like finally in gradle? I want to do some clean up, no matter whether task succeeds or fails


(vipss78) #1

Is there anything like finally in gradle? I mean… execute doLast even if task fails, or no matter what happens but execute this part of code…


(René Groeschke) #2

Hello, at the moment there is nothing like finally in gradle, but something similar is definitely planned. Have a look at this post http://forums.gradle.org/gradle/topics/improving_tasks-19op1r for the details.

cheers, René


(vipss78) #3

Any hack arounds that I can use? Does groovy/java try catch finally works? Since its a Gradle task, I may not be able to control it using those commands


(Peter Niederwieser) #4

If the task implementation is your own, you can use try-finally. If it is an existing task class, you can subclass it, override its @TaskAction method and use try-finally (calling into the super method). If this is about cleaning up files, you can write them to the build output directory and rely on the ‘clean’ task for cleanup. Or you can reconfigure the ‘clean’ task to delete other files as well.


(Hans Dockter) #5

There is a finally in Gradle. Within your build script you can say:

gradle.taskGraph.afterTask { task, TaskState state ->
   if (task == myTask) {
      // clean up
   }
}

You can also change the task state to do conditional stuff based on how the execution went.


(vipss78) #6

Thank you for all your responses…


(vipss78) #7

How to do the clean up even if the dependencies are failing…

more details are logged in different issue here… http://forums.gradle.org/gradle/topics/how_to_clean_up_after_task_b_is_done_success_or_failure_but_should_do_some_cleanup