Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 442
Bohemian Avatar asked Dec 14 '15 16:12

Bohemian


People also ask

What is FileTree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

What is .Gradle directory?

The Gradle user home directory ( $USER_HOME/.gradle by default) is used to store global configuration properties and initialization scripts as well as caches and log files.

What is task in Gradle?

The work that Gradle can do on a project is defined by one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.

What is gradle W?

gradlew , gradlew.bat. A shell script and a Windows batch script for executing the build with the Wrapper. You can go ahead and execute the build with the Wrapper without having to install the Gradle runtime. If the project you are working on does not contain those Wrapper files then you'll need to generate them.


2 Answers

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

like image 68
AdamSkywalker Avatar answered Oct 24 '22 00:10

AdamSkywalker


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.

like image 20
Alla B Avatar answered Oct 23 '22 22:10

Alla B