Using the copy task correctly


(Sean Van Buggenum) #1

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!

sean

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'
}
testPublishDBUpdate.doFirst{
 println "attempting to publish db update files...."
}
testPublishDBUpdate.doLast{
 println "finished db file update"
}
      task testFinalize(dependsOn: [testPublishDBUpdate]) << {
  println "in testFinalize"
}

(Sean Van Buggenum) #2

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'
    doLast{
 // any other stuff I have to do... which might be depended on by another task
     println " the args are: "
  }
}

Hows that?


(Peter Niederwieser) #3

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}”.


(Sean Van Buggenum) #4

ah ok, so simply

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

?


(Peter Niederwieser) #5

Yes. You can also omit the brackets around testCheckArgs.