Bohemian
Bohemian

Reputation: 424983

How to copy a directory using Copy task in gradle

I want to copy a range of files and whole directories to another directory in a single Copy task. I can copy individual files, and the contents of directories, but how do I copy the directory itself?

This is my task:

task myTask(type: Copy) {
    from 'path/to/file'
    from 'path/to/dir'
    into 'path/to/target'
}

which copies the file OK, but only the files in the dir. I want to end up with the contents of the dir in path/to/target/dir (not in path/to/target).

I found a work around by using:

task myTask(type: Copy) {
    from 'path/to/file'
    from 'path/to'
    into 'path/to/target'
    include 'dir'
}

But that is prone to name collisions. I actually have many files and dirs to copy, and I want to make it one task.

Upvotes: 13

Views: 9933

Answers (2)

Alla B
Alla B

Reputation: 676

There is a book that is available for download on the Gradle website here or click "Get Free Ebook" here: "Gradle Beyond the Basics" which provides a direct answer to your question on page 3 (Transforming Directory Structure).

Applied to your example the solution would be as follows:

    task myTask(type: Copy) {
        from 'path/to/file'
        from 'path/to/dir' {
            into 'dir'
        }
        into 'path/to/target'
    }

The limitation is that it will not dynamically determine the name of the second level target directory (dir) from the source but it is a clean approach.

Upvotes: 13

AdamSkywalker
AdamSkywalker

Reputation: 11609

The only solution I know for your problem:

task myTask(type: Copy) {
    into 'path/to/target'
    from 'path/to/file'

    into ('dir') {
       from 'path/to/dir'        
    }
}

Keep in mind that into: ('dir') construction works relatively to path/to/target/ location

Upvotes: 11

Related Questions