Create zip files in a loop


(missourian55) #1

I want to create 1aa.zip, 2aa.zip and 3aa.zip, but all I get is 3aa.zip. How to loop and create three different zip files? I am a gradle newbie, any advice please. Thanks in advance

task myZip(type: Zip) {
    List a=[1,2,3]
    for(int num:a) {
        myZip {
            from '/Users/sbam/repos/Config/myfolder'
            include '*.*'
            destinationDir new File('/Users/sbam/repos/Config/myfolder/out')
            archiveName num+"aa.zip"
          }
    }
}

(Luke Daley) #2

Each zip task can only create one zip file. You will need to create multiple tasks.


(Gary Hale) #3

Try this:

List a=[1,2,3]
a.each { num ->
    task "myZip$num"(type: Zip) {
            from '/Users/sbam/repos/Config/myfolder'
            include '*.*'
            destinationDir new File('/Users/sbam/repos/Config/myfolder/out')
            archiveName num+"aa.zip"
    }
}

(missourian55) #4

Thanks, sorry if this is a dump question, but how to invoke the above task, when I did gradle tasks it gives me myZip1,2 and 3


(Peter Niederwieser) #5

‘gradle myZip1’ or ‘gradle myZip2’ or ‘gradle myZip3’, depending on which task you want to execute. You can also add a composite task: ‘task allZips { dependsOn tasks.withType(Zip) }’.