I want to create a task that will zip up all the files in a directory, ignoring all subdirectories. At the same time, I want to iterate through all the subdirectories and create zips that include the files in those directories (and ignoring any further subdirectories).
Thus, for this directory structure, I want the following zips:
$ls build/dirA
fileA
dirB
$ls build/dirA/dirB
fileB
I should get zip/zipA containing fileA, zip/zipB containing fileB.
I am using code based on the answer here: gradle task to zip multiple directories independently Essentially, I create a zip task for each zip artifact to be created, with a dependency on the top level task.
task myZip() {
file('build').eachDir { dir ->
dir.eachDir { sub ->
dependsOn tasks.create("zip$sub.name", Zip) {
from sub
baseName sub.name
destinationDir file('zip')
}
}
dependsOn tasks.create("zip$dir.name", Zip) {
from dir
baseName dir.name
excludes ['*/**']
destinationDir file('zip')
}
}
}
The problem seems to be in the exclude pattern. I get the correct zips for the sub directories, but the parent directory includes fileA
and dirB/fileB
.
What is the correct way to do this please?
*Note I have not put an exclude for the iteration through the subdirectories, as I figure this pattern needs to be the same as for the parent directory, which I haven't figured out yet.