gradle task to zip multiple directories independently

夙愿已清 提交于 2019-12-10 10:14:17

问题


I am trying to create a task that looks inside a folder and zips all the folders within that folder to some output folder.

Initial state:

Folder1
    -> project1
        ->code //some more files within project1
    -> project2
        ->code

Target state:

Destination
    project1.zip
    project2.zip

I tried using the below code, but it is zipping all the content within the folder1

task myZip(type: Zip) {
   from 'Folder1'   
   archiveName 'Compress.zip'
   destinationDir file('build')
}

I probably might need to use some collections which contains the project1 and project2 info and iteratively zip to the destination folder. Having issues with this approach.


回答1:


Each Gradle Zip task can only create a single Zip file, as you can read here.

So, you need to create multiple tasks. Therefore, you can iterate over the subdirectories and create a new Zip task for each one of them. The generated tasks can be bundled in a root task via dependsOn:

task myZip() {
    file('Folder1').eachDir { sub ->
        dependsOn tasks.create("${name}_${sub.name}", Zip) {
            from sub
            baseName sub.name
            destinationDir file('Destination')
        }
    }
}


来源:https://stackoverflow.com/questions/43551572/gradle-task-to-zip-multiple-directories-independently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!