0

I'm currently studying gradle.

I have the following code

task simpleCopy(type: Copy){
    from 'source.xml'
    into 'destinationFolder'
}

My understanding is that the code inside {} is the configuration closure, and it is executed during the configuration phase, to prepare the task for execution during execution phase. So I'm expecting the source.xml to be copied into destinationFolder during the configuration phase (in other words, the copying will happen when I simply run gradle, and I dont have to specifically run gradle simpleCopy for the copying behavior to happen).

But what I have found is that the copying does NOT happen when I run gradle at command line. The copying only happens when I explicitly execute the simpleTask task (i.e. by running gradle simpleTask at command line). So the code above actually behave the same as

task simpleCopy(type: Copy){
    doLast {
        from 'source.xml'
        into 'destinationFolder'
    }
}

Is my understanding of configuration phase and configuration closure incorrect? Or am I missing some information?

halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • 1
    As Henry says, the closure configures the `from` and `into` properties of the copy task. They are then used to do the copying in the execution phase – tim_yates Mar 04 '18 at 08:58
  • hi Henry and @tim_yates, thanks for the heads up! that makes perfect sense :) – Thor Mar 04 '18 at 12:29
  • [As previously requested](https://stackoverflow.com/q/39095697): _Hi there Tony. I am seeing a lot of repeated "filler" text in your questions - just so you know, we tend to trim this out, so if you can omit Could someone please help me out? and Thanks in advance for any help and I don't know where else to ask this, it would save us quite a bit of editing work._ – halfer Mar 05 '18 at 15:50
  • @halfer thank you very much for editing my questions. Really appreciate what you are doing. Its just feels a bit rude on my part, if I ask people for help without saying thank you. So thats why I always add some text of appreciation to my question at the end. :) – Thor Mar 07 '18 at 00:40
  • Sure, I understand that. However, I am sure you will agree: once you know the community would rather you did not do that, it would be rude to continue doing it. References on Meta are many, here's [one](https://meta.stackoverflow.com/q/288160) and [here's another](https://meta.stackoverflow.com/q/260776). – halfer Mar 07 '18 at 00:42
  • 1
    @halfer. thanks for the word of advice. I will try my best to keep up with the guidelines :) – Thor Mar 07 '18 at 00:54

1 Answers1

3

During the configuration phase the copy task is configured, i.e. the source and destination locations are set (this is all the configuration closure does) but the copy itself is not yet done.

The copy only happens when the task is executed.

Henry
  • 42,982
  • 7
  • 68
  • 84