Friday, February 22, 2019

Task dependencies and setting properties to tasks

Task6.dependsOn  Task5
Task6.dependsOn Task3
Task5.dependsOn Task4

when we run Task6, Task5 executes first.

>gradle Task6


Or we can write inside the closure.

task Task6{
 description "This is task 6"
 depends Task5
 doFirst {
  println "Task 6 - First"
}
 doLast {
    println  "This is task 6"
  }

}


Setting properties in Tasks:
---------

def projectVersion = "2.0"

task Task6{
doLast{
  println "This is task 6 - version $projectVersion"
}

}

The variable scope above one is for the build file.
If we declare inside Task, it will be of Task scope.

To share it across project.use project object.

project.ext.projectVersion = "2.0"
The project keyword is not mandatory as it is the root one. So:  ext.projectVerion= "2.0"

we can refer it using $project.ext.projectVersion.If no variable declared locally, we can use directly. $projectVersion


--

Dependency with multiple tasks.



task taskA << {println"task A"}
task taskB << {println"task B"}
task taskC << {println"task C"}
task taskD << {println"task D"}
task taskE << {println"task E"}
task taskF << {println"task F"}

taskA.dependsOn taskB
taskA.dependsOn taskC, taskD

gradle -q taskA  ---- q for quiet logging.only our output , no gradle default output.

o/p:
task B
task C
task D
task A

if we change the order of depends flags and build again, it won't change the outptut as gradle ignores that.


Add below lines now,

taskC.dependsOn taskE
taskD.dependsOn taskE

o/p:
gradle -q taskA

B
E
C
D
A

task taskG
{
  dependsOn taskE
   doLast{
     println "task G"
}
}

taskA.dependsOn taskG

gradle -q taskA
o/p:
B
E
C
D
G
A


Other dependencies attributes:

mustRunAfter - If  2 tasks execute one must run after the other
shouldRunAfter - same as must rn but ignores circular dependencies.
finalizedBy - incubating in 2.6, Inverted dependency

taskB.mustRunAfter taskC

E
C
B
D
G
A

taskB.mustRunAfter taskC
taskB.shuldRnAfter taskD

E
D
C
B
G
A

task2.mustRunAfter task1

gradle -q task1 task2

forces task1 to run first even if we change order.




taskE.finalizedBy taskF

whenever we run taskE, it will trigger taskF after it.










No comments:

Post a Comment