Kip
Kip

Reputation: 109397

Copy entire directory in Gradle

I have a directory structure like this:

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

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

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

        into "mytest"
    }
}

But this results in the following:

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

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

Is it possible to do this directly with Gradle copy?

So far, I have only been able to come up with this solution:

task mytest << {
    copy {
        from "file1.txt"
        from "file2.txt"

        into "mytest"
    }

    copy {
        from "dir1"
        into "mytest/dir1"
    }
}

For my simple example there's not much to it, but in my actual case there are many directories I want to copy, and I'd like to not have to repeat so much.

Upvotes: 28

Views: 41310

Answers (4)

Will Calderwood
Will Calderwood

Reputation: 4634

I don't know how long this syntax has been around, but it seems a little clearer.

task copyToRelease(dependsOn: [deletePreviousRelease], type: Copy) {
    from('build/dist') {
        include '**/*.*'
    }
    destinationDir(new File('../htmlrelease/src/main/webapp/canvas')) 
}

Upvotes: 2

Ankush
Ankush

Reputation: 519

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

and created the following code (with Up-to-date check) based on it:

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

Upvotes: 4

Emanuel
Emanuel

Reputation: 47

Maybe also helpful: Usage of fileTree to copy an entire directory recursively, e.g.,

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

Upvotes: -2

Andrew Lygin
Andrew Lygin

Reputation: 6197

You can use . as the directory path and include to specify, 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'll end up with the full copy of the source directory in the destination directory.

Upvotes: 51

Related Questions