How to copy to multiple destinations with Gradle copy task?
If you really want them in one task, you do something like this:
def filesToCopy = copySpec {
from 'someFile.jar'
rename { 'anotherfile.jar' }
}
task copyFiles << {
['dest1', 'dest2'].each { dest ->
copy {
with filesToCopy
into dest
}
}
}
an alternative way
task myCustomTask << {
copy {
from 'sourcePath/folderA'
into 'targetPath/folderA'
}
copy {
from 'sourcePath/folderB'
into 'targetPath/folderB'
}
copy {
from 'sourcePath/fileA.java','sourcePath/fileB.java'
into 'targetPath/folderC'
}
}
With a Common Destination Base Path
If your destination paths share a common path prefix (dest_base
), then you can use something like this:
def filesToCopy = copySpec {
from 'somefile.jar'
rename { String fileName -> 'anotherfile.jar' }
}
task copyFile(type: Copy) {
into 'dest_base'
into('dest1') {
with filesToCopy
}
into('dest2') {
with filesToCopy
}
}
Compared to other answers which use the copy
method, this approach also retains Gradle’s UP-TO-DATE checks.
The above snippet would result in output like this:
dest_base/
├── dest1
│ └── anotherfile.jar
└── dest2
└── anotherfile.jar