Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle doLast, doFirst behavior in copy task

Tags:

copy

gradle

task

The

build.gradle

task hello (type: Copy) {
   doLast {
      println "print from within"
   }
   println "print from outside"
}

when run

gradle -q hello

the doLast{} closure is not even touched. change doLast to doFirst, I get the same result:

D:\>gradle -q hello
print from outside

without the doLast or doFirst:

task hello (type: Copy) {
    //doLast{ 
       println "print from within"
    //}
    println "print from outside"
}

it works fine:

D:\>gradle -q hello
print from within
print from outside

This seems happening only with tasks of type Copy. can you please help clarifying? I am using gradle 2.4.

like image 934
Heinz Avatar asked Dec 06 '16 18:12

Heinz


2 Answers

I thought it would be helpful for the people who come to this place on the same question. Though the above explanation is conceptually making sense. There is no proper solution mentioned in the answer. Here is the solution on how i achieved Copy task with doLast execution.

  task hello () {
        doLast{ 
           copy {
             println "print from within"
           }
        }
        println "print from outside"
    }

This would provide the execution of copy task along with doLast.

$ gradle -q hello print from outside print from within

Hope this is useful.

like image 56
Vali Shah Avatar answered Nov 26 '22 11:11

Vali Shah


If you run the task without -q switch you'll notice that the task is already marked as UP-TO-DATE - which means that no actions (and you add an action via doLast) were executed.

Why is that? In this particular example, you have configured no inputs and outputs for the task. Copy tasks resolve it's being up-to-date base on the inputs and outputs and since none were configured Gradle assumes there's no need for it to be run.

like image 41
Opal Avatar answered Nov 26 '22 11:11

Opal