Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the design purpose of Gradle doFirst and doLast?

Tags:

gradle

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?

like image 745
Liu Nate Avatar asked Oct 04 '16 09:10

Liu Nate


People also ask

What does doLast do in Gradle?

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.

What is afterEvaluate in Gradle?

> 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.

What is Buildscript in build Gradle file?

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.

What does Gradle use to determine the order in which tasks can be run?

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.


1 Answers

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.

like image 57
Alpar Avatar answered Sep 17 '22 12:09

Alpar