How to call task execution from outside task?

I have code:

gradle.buildFinished {buildResult ->
    println "Run cleanup buildFinished(). BuildResult: ${buildResult.dump()}"

Gradle 4.5.1 warn about it as The TaskInternal.execute() method has been deprecated and is scheduled to be removed in Gradle 5.0. There are better ways to re-use task logic, see
But link does not give me any hint how I could replace it because I can’t just setup task dependencies like dependsOn or finalizdBy - it called not from other task but from end of build.

By calling execute() directly, you’re by-passing all of Gradle’s task execution infrastructure.

Based on the name of the task, I assume that the task doesn’t really have any inputs/outputs and you just need it to do some work as clean-up from other things (some sort of container teardown?). There are a few alternatives you can use:

  1. Make the tasks that rely on the “EgaisappContainer” or “EgaisdbContainer” to be finalizedBy the remove tasks. This might be faster than what you’re doing now because Gradle could run these tasks in parallel or earlier than the end of the build.
  2. Stop using tasks and call some kind of utility-method version of these tasks to do their work. This is the closest equivalent to what you’re doing now, but it looks like a smell since you’re always doing this work unnecessarily (e.g., when someone runs gradle tasks)
1 Like

Thank you! I found way do it with finalizedBy