Reputation: 2720
Warning: Gradle newbie... :-)
I'm in the process of converting an existing project from Ant to Gradle. A large portion of my project consists of code that is generated. This code is generated into an internal directory and a local build is done to ensure that the code is generated correctly. Once the local test build is verified, the generated code is copied to a set of WAR projects for final deployment.
So as part of the build, I have a task that copies files from the test build to a WAR project. While making the copy there are a few files that should only be copied if they don't already exist in the destination WAR project. To do this, I wrote the following Gradle copy task:
task copyServer (type: org.gradle.api.tasks.Copy) {
group = 'ds_copy'
String serviceRootDir = "${DS_ROOT_DIR}/NDMS_DS_Generated_Services/server_src/${x_dsServiceDir}"
String serviceDestDir = "${DS_ROOT_DIR}/NDMS_DS_Client/srv_src/${x_dsServiceDir}"
FileTree setupFiles = fileTree ("$serviceRootDir") {
include "gen/**/*.java", "request/gen/**.java"
}
doFirst {
File appFile = new File (serviceDestDir, "${dsName}App.java")
if (appFile.exists () == false)
{
setupFiles = fileTree (dir: "$serviceRootDir", include: ["gen/**/*.java", "request/gen/**.java", "${dsName}App.java"])
}
println ("Copying service classes for ${dsName} from ${serviceRootDir}")
println ("Files to copy: ")
setupFiles.each { println (it) }
println (setupFiles)
}
from (setupFiles)
into (serviceDestDir)
doLast {
println ("Finished copying service classes for ${dsName}")
}
}
My problem is that I can't seem to update the fileTree from the doFirst block in order to make the check for the existence of the files in the destination. Even though I see setupFiles getting updated, when the copy occurs the file I added in the doFirst block does not get copied.
Is there any way to modify the fileTree that the copyTask is using?
Upvotes: 1
Views: 1581
Reputation: 38734
Never ever try to change the configuration of a task from the execution phase, especially if this changes the inputs or outputs. This will break the up-to-date checks and I think Gradle in recent versions even actively prohibits this.
Make a second task that generates your file and then make the copyServer task include the output of that task via configuration. You should not even need an explicit task dependency if you did it correctly, as it shoudl be added implicitly.
Actually your copy task should more look like
task copyServer(type: Copy) {
group = 'ds_copy'
from serviceRootDir
into serviceDestDir
include 'gen/**/*.java'
include 'request/gen/**.java'
include "${dsName}App.java"
doLast {
println "Finished copying service classes for $dsName"
}
}
Btw. one tip for the Ant -> Gradle conversion. Learn Gradle, forget your Ant buildscript, create your build from scratch with Gradle in the Gradle way. If you try to port your existing Ant build, you will most likely end up with very ugly Gradle builds that might also be much less efficient than they could be.
Upvotes: 1