avalon avalon - 2 months ago 22
Java Question

Java Gradle copy 2 different folders in one task

I use gradle-2.8 on java project in intelliJidea. I need to copy 2 different resources into 2 different folders.

I tried:

task copySubprojectLibs (type: Copy, dependsOn: subprojects.jar) {
from (subprojects.jar) {
into 'build/libs/lib'
}

from ('src/main/resources') {
into 'build/libs/nosr/conf'
}
}


Then tried:

task copySubprojectLibs (type: Copy, dependsOn: subprojects.jar) {
from (subprojects.jar) into 'build/libs/lib'
from ('src/main/resources') into 'build/libs/nosr/conf'
}


Then tried to remove dependsOn, type arguments, reorder lines, etc, but I always get

> No value has been specified for property 'destinationDir'.


What I have to do to copy 2 resources to 2 different folders? In one task or in 2 tasks, this stuff doesn't work in two tasks either. But simple copy in one line and one task works. How to do that in one task? It's a simple problem, why gradle that tricky?

Answer

The Copy task is configured with a CopySpec, which only allows one destination directory.

Consider this approach instead:

task copySubprojectLibsA (type: Copy, dependsOn: subprojects.jar) {
    from subprojects.jar 
    into 'build/libs/lib'
}

task copySubprojectLibsB (type: Copy, dependsOn: subprojects.jar) {
    from 'src/main/resources'
    into 'build/libs/nosr/conf'
}

task copySubprojectLibs(dependsOn: ["copySubprojectLibsA","copySubprojectLibsB"])

An alternative is to use the Ant Builder contained in Gradle:

task copySubprojectLibs (dependsOn: subprojects.jar) << {
    files(subprojects.jar).each { 
        ant.copy(file: it.absolutePath, todir: "build/libs/lib")
    }
    ant.copy(todir: "build/libs/nosr/conf") {
        fileset(dir: "src/main/resources")
    }
}
Comments