使用Gradle从多个目录动态创建多个拉链

使用Gradle从多个目录动态创建多个拉链,gradle,build,zip,Gradle,Build,Zip,我的构建中有一个相对复杂的目录结构,我想从中创建多个ZIP文件。结构如下: + project root + -- build.gradle + -- + foo + -- bar-1 | + -- bar-1.a.json | + -- bar-1.a.xml | + -- bar-1.b.json | + -- bar-1.b.xml |

我的构建中有一个相对复杂的目录结构,我想从中创建多个ZIP文件。结构如下:

+ project root
    + -- build.gradle
    + -- + foo
         + -- bar-1
         |   + -- bar-1.a.json
         |   + -- bar-1.a.xml
         |   + -- bar-1.b.json
         |   + -- bar-1.b.xml
         |
         + -- bar-2
         |   + -- bar-2.a.json
         |   + -- bar-2.a.xml
         |   + -- bar-2.b.json
         |   + -- bar-2.b.xml
         |
         + ...
         + -- bar-n
             + -- bar-n.a.json
             + -- bar-n.a.xml
             + -- bar-n.b.json
             + -- bar-n.b.xml
从这个结构中,我想为每个
bar-x
目录创建ZIP文件,例如,从
foo/bar-1
目录,应该在
out/foo/bar-1.ZIP
中创建ZIP文件,从
foo/bar-2
中创建ZIP文件,等等

重要的是,以后可能会出现新目录,因此我不能硬编码名称,Gradle应该在每个构建中列出
foo
中的目录


有人能给我一个如何实现这一点的示例吗?

类似于以下内容的内容应该可以做到:

task zipAll
file('foo').eachDirMatch(~/bar-.*/) { barDir ->
    def taskName = "zip${barDir.name.capitalize()}"
    task "$taskName"(type: Zip) {
        from barDir
        destinationDir file('out/foo/')
        baseName barDir.name
    }
    zipAll.dependsOn taskName
}

类似于以下的操作应该可以做到:

task zipAll
file('foo').eachDirMatch(~/bar-.*/) { barDir ->
    def taskName = "zip${barDir.name.capitalize()}"
    task "$taskName"(type: Zip) {
        from barDir
        destinationDir file('out/foo/')
        baseName barDir.name
    }
    zipAll.dependsOn taskName
}

完美的解决方案,非常感谢。动态添加任务确实是一个不错的解决方案。:)完美的解决方案,非常感谢。动态添加任务确实是一个不错的解决方案。:)