Conventional way of copying files in Gradle - use Copy task or copy method?

Go To StackoverFlow.com

28

I'm adding a task to deploy war files to Tomcat .. the only thing that the task needs to do is copy the war file to the TOMCAT location.

There 2 ways that I can think of implementing this .. but being new to gradle, I'm not quite sure what's more conventional/right (or if it even matters).

task myCopy(type: Copy)

    myCopy.configure {
       from('source')
       into('target')
       include('*.war')
    }

or

task myCopy{
  doLast{
     copy {
       from 'source'
       into 'target'
       include '*.war'
     }   
  }

}
2012-04-03 21:38
by vicsz


37

In most cases (including this one), the Copy task is the better choice. Among other things, it will give you automatic up-to-date checking. The copy method is meant for situations where (for some reason) you have to bolt on to an existing task and cannot use a separate task for copying.

The code for your Copy task can be simplified to:

task myCopy(type: Copy) {
    from('source')
    into('target')
    include('*.war')
}
2012-04-03 22:36
by Peter Niederwieser
Thanks ... I understand the advantages of automatic up-to-date checking, what are the other advantages - vicsz 2012-04-04 17:39
Better style (a task should do one thing and should be explicit about it), better configurability (e.g. from a parent build script and/or using a configuration rule) - Peter Niederwieser 2012-04-05 03:25
For some reason this task doesn't run by default - IgorGanapolsky 2016-07-29 15:38
Ads