In Gradle, I'm trying to copy a list of absolute paths into a single directory, but I want to keep the last component of the absolute path as a subfolder of the destination directory.
Currently I have:
def copyTask = tasks.create(name: "copyFolders", type: Copy, overwrite: true) {
from ["/sources/a-1/first", "/sources/a-3/b-1/c-1/second"]
into "/destination"
}
but this results in the files contained in /sources/foo/bar and /sources/one/two/three/path being copied into /destination. Instead, I want /destination to contain both bar and path folders (with their contents inside them).
It should be able to handle this case correctly:
sources
├── a-1
│ └── fist
│ └── lorem.txt
| └── subfolder
│ └── lorem2.txt
├── a-2
│ └── fist-b
│ └── lorem.txt
| └── subfolder
│ └── lorem2.txt
└── a-3
└── b-1
└── c-1
└── second
└── ipsum.log
->
destination
├── first
│ └── lorem.txt
│ └── subfolder
│ └── lorem2.txt
└── path
└── ipsum.log
Based on @szymon answer I got:
def copyTask = tasks.create(name: "copyToDestDir", type: Copy, overwrite: true) {
includeEmptyDirs = false
def fromDir = "/sources"
def sourcePaths = ["/sources/a-1/first", "/sources/a-3/b-1/c-1/second"]
from fileTree(fromDir).dir.listFiles()
eachFile { copySpec ->
def shouldBeCopied = false
sourcePaths.find {
if (copySpec.getFile().getCanonicalPath().startsWith(it)) {
shouldBeCopied = true
return true // break iteration
}
}
if (shouldBeCopied) {
String[] parts = file.path.split('/')
copySpec.path = parts.size() > 1 ? "${parts[parts.size() - 2]}/${parts[parts.size() - 1]}" : parts[0]
} else {
copySpec.exclude()
}
}
into destDir
}
but this wrongly results in:
destination
├── first
│ └── lorem.txt
└── subfolder
│ └── lorem2.txt
└── path
└── ipsum.log
How to achieve this?