Best way to copy two directories from the project directory?


(Cymbaline Austin) #1

I want to copy the src and gradle folders, including their contents, from the project’s home directory.

Example:
ProjectFolder/
src/ gradle/

src/
main/ test/

gradle/
wrapper/

I came up with one solution but I have to list everything I don’t want which I don’t think is a great solution.
Include doesn’t work

task copySource(type: Copy, description: "Copies files to prepare for archival"){
from ("${projectDir}/"){
    exclude 'build', 'bin'
}
into "${buildDir}/libs/"
}

This option works well too but only with one directory

task copySource(type: Copy, description: "Copies files to prepare for archival"){
from ('src')
from ("${buildDir}/libs") { 
     include 'main/*', 'test/*'
     }
into "${buildDir}/libs/src"
}

I ended up creating separate tasks to accomplish what I need but is there a way to do this all in one task?

task copySource(dependsOn:'fatJar',type: Copy, description: "Copies files to prepare for archival"){
from ('src')
into "${buildDir}/libs/src"
}
task copyReadme(dependsOn:'copySource', type: Copy, description: "Copies files to prepare for archival"){
from ('README.md)
into "${buildDir}/libs/
}

task copyGradle(dependsOn:'copyReadme', type: Copy, description: "Copies files to prepare for archival"){
from ('gradle')
into "${buildDir}/libs/gradle"
}

(James Justinic) #2

You mentioned that “include” doesn’t work, but I don’t see an example of what you tried. Using includes, you should be able to use the projectDir as the source but you need to match all files/folders in the folder (i.e. src and gradle need to be src/** and gradle/**). A single * won’t work. All three tasks as one task:

task copySource(type: Copy, dependsOn: 'farJar', description: 'Copies files to prepare for archival') {
    from projectDir
    include 'src/**', 'gradle/**', 'README.md'
    into "${buildDir}/libs"
}

(Cymbaline Austin) #3

You mentioned that “include” doesn’t work, but I don’t see an example of what you tried.

It’s in the second to last section of code. My fault, I have so much stuff listed it’s easy to miss.
However now I’m trying to follow your other piece advice and JUST use archive and see if I can accomplish archiving these folders.