Using the copy task correctly

Hi all,

I’m having a bit of a problem.

In my gradle script, I have some global variables.

One of these variables is used to set the ‘from’ parameter of the copy task,

yet, that parameter is a derived parameter, which is itself set within a task.

The problem seems to be because the copy task is all setup during the configuration phase (including setting the from parameter) before the variable itself is set.

And so, the copy task always reports ‘UP-TO-DATE’ … because there is no valid ‘from’ parameter.

I am probably not thinking about this in the right way, hence the problem in the first place…

I’ll paste my example below… How best to re-write this?

thanks in advance for any help!


Run the script via :

gradle testFinalize -Prelease_output=C:/MyCopyToPath

def testBuildHome
  task testCheckArgs() << {
  testBuildHome = 'C:/MyWouldBeDerivedFromPath'
    tasks.add(name: 'testPublishDBUpdate', type: Copy, dependsOn: [testCheckArgs]) {
  println "in the creation of the task"
 from "${testBuildHome}"
 into "${release_output}"
 include 'update.jar'
 include 'update_*.jar'
 println "attempting to publish db update files...."
 println "finished db file update"
      task testFinalize(dependsOn: [testPublishDBUpdate]) << {
  println "in testFinalize"

Actually, I guess I kinda answered the question for myself, in writing it down.

I don’t do this (gradle) often enough for it to stay fresh;

but I guess what I need to do is move the variable setting to the ‘configuration phase’ part of the testCheckArgs function… that is:

task testCheckArgs() {
  testBuildHome = 'C:/MyWouldBeDerivedFromPath'
 // any other stuff I have to do... which might be depended on by another task
     println " the args are: "

Hows that?

I’d initialize the variable right at the point of declaration. But yes, you found the cause of the problem.

Syntax-wise, I suggest to use task foo instead of tasks.add(“foo”), and from testBuildHome instead of from “${testBuildHome}”.

ah ok, so simply

task testPublishDBUpdate(type: Copy, dependsOn: [testCheckArgs]) {
    from testBuildHome
    into release_output
    include 'update.jar'
    include 'update_*.jar'


Yes. You can also omit the brackets around testCheckArgs.