Gradle task "tasks" for displaying all tasks executes all tasks, which have at least one dependent task


(Alexander Bartash) #1

I’m using gradle 1.7. OS Kubuntu: Linux 3.8.0-27-generic #40-Ubuntu SMP Tue Jul 9 00:17:05 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

Consider the following build script

task taskX {

println ‘Drop all data in production database and format hard drive.’ }

task taskY(dependsOn: ‘taskX’) << {

println ‘Quit your job’ }

then run it with next command: gradle tasks

Given output:

Drop all data in production database and format hard drive. &lt=== Why? :tasks …


(Attila Kelemen) #2

You forgot to add “<<” to ‘taskX’. Therefore its closure is executed at evaluation time and is not an action of ‘taskX’.


(Alexander Bartash) #3

Oh, yes, thank you! My code is differ from that i wrote above, but I realized my mistake there too. I’m passing the closure as the parameter instead of the parent task, in that case, given closure always executes by the task “tasks”, strange behaviour… I’m doing this, because I need to add dependecy from taskA too taskB, but when taskB is executed implicitly thru the dependency, it must behave in a little different way. May be it’s better to use ‘doFirst’ feature.

  apply plugin: TestTaskPlugin

 class TestTaskPlugin implements Plugin {



  @Override



void apply(Project project) {





 def taskXClosure = {







  println 'Drop all data in production database and format hard drive.'





 }







Task taskX = project.task("taskX") << taskXClosure





 Task taskY = project.task("taskY") << {







  println 'Quit your job'





 }





 taskY.dependsOn taskXClosure



}  }  

(Alexander Bartash) #4

Oh, yes, thank you! My code is differ from that i wrote above, but I realized my mistake there too. I’m passing the closure as the parameter instead of the parent task, in that case, given closure always executes by the task “tasks”, strange behaviour… I’m doing this, because I need to add dependecy from taskA too taskB, but when taskB is executed implicitly thru the dependency, it must behave in a little different way. May be it’s better to use ‘doFirst’ feature.

  apply plugin: TestTaskPlugin

 class TestTaskPlugin implements Plugin {



  @Override



void apply(Project project) {





 def taskXClosure = {







  println 'Drop all data in production database and format hard drive.'





 }







Task taskX = project.task("taskX") << taskXClosure





 Task taskY = project.task("taskY") << {







  println 'Quit your job'





 }





 taskY.dependsOn taskXClosure



}  }  

(Attila Kelemen) #5

In the above code ‘taskY.dependsOn taskXClosure’ should be ‘taskY.dependsOn taskX’. That is, you can’t depend on a closure (the closure will be evaluated and its return value will be used).

I haven’t tested what you wrote but I believe that ‘taskXClosure’ will return ‘null’ and ‘dependsOn’ silently ignores ‘null’ values.


(Attila Kelemen) #6

Depending on what exactly is “little different”, you should find another way to implement what you want. For example, there could be two separate tasks and you could create method to initialize (or create) the tasks in a slightly different way (as needed).


(Alexander Bartash) #7

I see, thanks!


(Alexander Bartash) #8

I see, thanks!