Copy the entire directory into Gradle

I have a directory structure like this:

file1.txt file2.txt dir1/ file3.txt file4.txt 

I want to use Gradle to copy the entire structure to another directory. I tried this:

 task mytest << { copy { from "file1.txt" from "file2.txt" from "dir1" into "mytest" } } 

But this leads to the following:

 mytest/ file1.txt file2.txt file3.txt file4.txt 

See, the copy from dir1 copied the files to dir1 , whereas I want to copy dir1 myself.

Is it possible to do this directly using Gradle copy ?

So far I have managed to find this solution:

 task mytest << { copy { from "file1.txt" from "file2.txt" into "mytest" } copy { from "dir1" into "mytest/dir1" } } 

For my simple example, there are not many, but in my actual case there are many directories that I want to copy, and I would not want to repeat so many.

+6
source share
3 answers

You can use . as a directory path and include to indicate which files and directories you want to copy:

 copy { from '.' into 'mytest' include 'file*.txt' include 'dir1/**' } 

If both from and into are directories, you will get a full copy of the source directory in the destination directory.

+14
source

I know this is a little late, but I tried @Andrew's solution above and it copied everything inside the directory. "" is not currently required to represent the direct in gradle. So I did some research and found this

and created the following code on it (with updated verification):

task resources Copy () {

 doLast { copy { from "src/main/resources" into "./target/dist/WEB-INF/classes" } copy { from "GeoIP2.conf" into "./target/dist/WEB-INF" } } 

}

0
source

Maybe also useful: using fileTree to recursively copy an entire directory, for example,

 task mytest << { copy { from fileTree('.') into "mytest" } } 
-one
source

All Articles