I have a list of file paths (say, ‘myFiles’), and I want to create a ‘Zip’ task that will zip them while preserving the directory hierarchy. The naive way (‘from(myFiles)’) dumps all files into the root of the archive - not what I want.
Here’s what I ended up with (this is a simplification of the actual code):
from(projectDir) {
def myDirectories = new HashSet<RelativePath>()
myFiles.each {
def parent = it.parent
while (parent != null) {
myDirectories.add(parent)
parent = parent.parent
}
}
include { fte ->
myFiles.contains(fte.relativePath) || myDirectories.contains(fte.relativePath)
}
}
This works, but it feels wrong. I’m essentially telling Gradle to zip everything and then filter all files except the one I want. Plus, it’s awfully verbose for such a seemingly simple task.
I’ve also thought of perhaps looping over the list of files and calling ‘from’/‘into’ for each of them, but that too feels like a hack.
So is there a simpler way of doing this?