将多个Gradle zip任务合并为一个任务。

huangapple go评论82阅读模式
英文:

Combining multiple gradle zip tasks into a single one

问题

我有一个Gradle任务,用于将.yaml文件压缩到一个zip文件夹中,代码如下所示:

task gradleTask1(type: Zip) {
    from 'src/test/resources/source_one/'
    include '**/*'
    archiveName 'file-collect.zip'
    destinationDir(file("${buildDir}/resources/test/staging/target_one"))
}

类似地,我还有其他相同的任务,但源目录和目标目录不同。

task gradleTask2(type: Zip) {
    from 'src/test/resources/source_two/'
    include '**/*'
    archiveName 'file-collect.zip'
    destinationDir(file("${buildDir}/resources/test/staging/target_two"))
}

task gradleTask3(type: Zip) {
    from 'src/test/resources/source_three/'
    include '**/*'
    archiveName 'file-collect.zip'
    destinationDir(file("${buildDir}/resources/test/staging/target_three"))
}

主要问题是,每次我添加新的zip任务时都必须添加依赖关系,如下所示:

compileJava.finalizedBy gradleTask1

compileJava.finalizedBy gradleTask2

compileJava.finalizedBy gradleTask3

是否有办法可以动态实现这些步骤?我能否拥有一个单一的zip任务(类似于zipAll),最终任务依赖关系可以是:

compileJava.finalizedBy zipAll

注意:我已经按你的要求只返回翻译的部分,没有包含其他内容。

英文:

I have a gradle task to zip the .yaml files into a zip folder, which looks like this:

task gradleTask1(type: Zip) {
    from 'src/test/resources/source_one/'
    include '**/*'
    archiveName 'file-collect.zip'
    destinationDir(file("${buildDir}/resources/test/staging/target_one"))
}

Similarly, I have other tasks that look the same, but the source and target directories are different.

task gradleTask2(type: Zip) {
    from 'src/test/resources/source_two/'
    include '**/*'
    archiveName 'file-collect.zip'
    destinationDir(file("${buildDir}/resources/test/staging/target_two"))
}

task gradleTask3(type: Zip) {
    from 'src/test/resources/source_three/'
    include '**/*'
    archiveName 'file-collect.zip'
    destinationDir(file("${buildDir}/resources/test/staging/target_three"))
}

And the main issue is I have to add dependency every time I add a new zip task as follows:

compileJava.finalizedBy gradleTask1

compileJava.finalizedBy gradleTask2

compileJava.finalizedBy gradleTask3

Is there any way I can achieve these steps dynamically? Can I have a single zip task (something like zipAll) and finally the task dependency can be

compileJava.finalizedBy zipAll

答案1

得分: 1

考虑以下内容(示例在此处):

tasks.withType(Zip).all { task ->
    def taskName = task.name
    
    if (taskName ==~ /gradleTask.*/) {
        println "TRACER adding dependency on ${taskName}"
        compileJava.finalizedBy taskName
    }
}

这将动态查找名称匹配gradleTask*的类型为Zip的任务,并将其添加到compileJava.finalizedBy的任务列表中。

虽然没有zipAll任务,但gradle compileJava将按预期工作。

英文:

Consider the following (example here):

tasks.withType(Zip).all { task ->
    def taskName = task.name

    if (taskName ==~ /gradleTask.*/) {
        println "TRACER adding dependency on ${taskName}"
        compileJava.finalizedBy taskName
    }
}

This will dynamically find tasks of type Zip with name matching gradleTask* and add it to the list of tasks for compileJava.finalizedBy.

There is no zipAll task, but gradle compileJava will work as desired.

huangapple
  • 本文由 发表于 2020年10月16日 02:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64377316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定