What is the purpose of Gradle doFirst and doLast design?

For example, I have a 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 is the design goal of doFirst and doLast, like any of the above tasks, gives the same result?

+7
gradle
source share
1 answer

This is due to extensibility, reuse and the prevention of duplication.

For one built-in task, you can continue, for example:

 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 consists of creating several projects where you can have a task definition at the top of the project using common behavior:

 allprojects { task myTask_a { doFirst {...} } } 

And then specific projects can expand that. The task essentially has a Closures list to run, and a choice of doFirst or doLast at which end of the insert list.

+9
source share

All Articles