gradle任务独立地压缩多个目录 [英] gradle task to zip multiple directories independently

查看:434
本文介绍了gradle任务独立地压缩多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个查看文件夹内的任务,并将该文件夹中的所有文件夹压缩到某个输出文件夹。

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

初始状态:

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

目标状态

Destination
    project1.zip
    project2.zip

我尝试使用下面的代码,但是它压缩了folder1中的所有内容1

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')
}

我可能需要使用一些包含project1和project2信息的集合,并迭代地压缩到目标文件夹。

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.

推荐答案

每个Gradle Zip 任务只能创建一个Zip文件,你可以阅读这里

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

所以,你需要创建多个任务。因此,您可以迭代子目录,并为它们中的每一个创建一个新的 Zip 任务。生成的任务可以通过 dependsOn 绑定在根任务中:

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')
        }
    }
}

这篇关于gradle任务独立地压缩多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆