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.
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With