How to call task execution from outside task?


(Pavel Alexeev) #1

I have code:

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

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 https://docs.gradle.org/4.5.1/userguide/custom_tasks.html#sec:reusing_task_logic.
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.


(Sterling Greene) #2

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)

(Pavel Alexeev) #3

Thank you! I found way do it with finalizedBy