For example I have the Gradle script like:
myTask_A { doFirst { println "first string" } doLast { println "last string" } }
The following two tasks have exactly the same execution result:
myTask_B { doFirst { println "first string" println "last string" } } myTask_C { doLast { println "first string" println "last string" } }
What's the design purpose of the the doFirst & doLast as any of above tasks produces the same result?
Task doLast ( Closure action)Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.
> gradle -q test Adding test task to project ':project-a' Running tests for project ':project-a' This example uses method Project. afterEvaluate() to add a closure which is executed after the project is evaluated. It is also possible to receive notifications when any project is evaluated.
buildscript: This block is used to configure the repositories and dependencies for Gradle. dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project.
In Gradle, task execution order is automatically determined taking into account explicit dependencies and implicit dependencies, and a specific execution order for the tasks that declared dependencies among themselves is not guaranteed. The only thing that is guaranteed is that the dependencies will be honored.
It has to do with extensibility, reuse and avoiding duplication.
For one built in tasks can be extended like:
task CopyAndThen(type: Copy) { doFirst { println "this is before the actual copy" } doLast { println "this is after the actual copy" } }
The second common scenario that comes to mind is with multi project builds, where you can have a task definition at the top of the project with common behavior:
allprojects { task myTask_a { doFirst {...} } }
And then the specific projects can extend that. The task essentially has a list of the Closures that needs to be run and the choice of doFirst
or doLast
controls to which end of the list the insert goes to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With