英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论