Multiple copies in a single copy task works a bit weird


(Richi) #1

Hello everyone

I’ve been working with Gradle for a few days so I’m kind of newbie.

In a bigger Gradle file, I have a task to copy some files from different folders (each file: different origin, different destination). I already checked that creating a single task for each copy, works but I was looking for a more “clean” way to do it so I came with this:

task copyFiles  (type: Copy, dependsOn: otherTask) {
copy {
	from ("${buildDir}/wrapper-linux-${jswArc}-${jswVersion}/bin/wrapper")
	into felix_tree + "/bin"
}
copy {
	from ("${buildDir}/wrapper-linux-${jswArc}-${jswVersion}/lib/libwrapper.so")
	into felix_tree + "/lib"
}
copy {
	from ("${buildDir}/wrapper-linux-${jswArc}-${jswVersion}/lib/wrapper.jar")
	into felix_tree + "/lib"
}
copy {
	from ("./felix")
	into felix_tree + "/bin"
}
copy {
	from ("./config.properties")
	into felix_tree + "/bin/conf"
}}

At the beginning I wasn’t using the copy{} structure, so all the files move to the first “into” destination, but using this structure Gradle works in a very weird way. The first time I execute the task, only the last two copy are made, and if I execute the same task immediately one more time, all the copies are made. And no idea why this is working like this

Probably my way is not the better way to do such task, but I don’t want to use five different task for such a simple actions. Is there any better way to do this?

Thanks


(uklance) #2

You are trying to use a Copy task but then you are invoking project.copy { ... } multiple times. You’re also doing your copies in the configuration phase instead of the execution phase which is always a bad idea.

I believe you want to do

task copyFiles (type: Copy, dependsOn: otherTask) { 
   with copySpec({ 
      from "${buildDir}/wrapper-linux-${jswArc}-${jswVersion}/bin/wrapper"
      into "$felix_tree/bin" 
   }) 
   ... 
} 

(Stefan Oehme) #3

You always need to give a base directory with a simple into call. Then you can have as many from/into pairs as you want.

task copyFiles  (type: Copy, dependsOn: otherTask) {
  into felixTree
  
  into("bin") {
    from  "${buildDir}/wrapper-linux-${jswArc}-${jswVersion}/bin/wrapper"
  }
  into("lib") {
    from "${buildDir}/wrapper-linux-${jswArc}-${jswVersion}/lib/libwrapper.so"
    from "${buildDir}/wrapper-linux-${jswArc}-${jswVersion}/lib/wrapper.jar"
  }
}

@Lance The with() method is useful when you want to create a reusable sub-structure. You still need to give a base directory.


(Richi) #4

Thanks!!

Working as expected, and in a clean way.