在我的 gradle 脚本中,我构建了一个运行 java 进程的任务。此过程取决于目标属性。任务定义为:
task('bulk', type: JavaExec, dependsOn: 'classes', description : 'Bulk data import on a target (defined by -Ptarget=[event|member|...]]') {
//available imports
ext{
event = relativePath('src/main/scripts/events.csv')
member = relativePath('src/main/scripts/member.csv')
membership = relativePath('src/main/scripts/membership.csv')
}
//check the target is set
doFirst {
if(!project.hasProperty('target')){
println "\nUsage:"
println "\tgradle bulk -Ptarget=[event|member|...]"
println "where target is : "
bulk.ext.each{ println it }
throw new GradleException('Target argument required')
} else {
println "\nBulk import of $target\n"
}
}
main = 'org.yajug.users.bulkimport.BulkImport'
classpath = sourceSets.main.runtimeClasspath
if(project.hasProperty('target')){
bulk{
args target
args bulk.ext[target]
debug false
}
}
}
并运行它:
gradle bulk -Ptarget=event
它工作正常,但知道我必须为不同的目标运行这个过程:
gradle bulk -Ptarget=event
gradle bulk -Ptarget=member
gradle bulk -Ptarget=membership
...
如何使用 gradle 的依赖模型将所有这些调用分组到另一个任务中?(我知道目标列表)
解决方案
task bulk;
['event','member','membership'].each {target ->
task("bulk${target}", type: JavaExec, dependsOn: 'classes', description : "Bulk data import of ${target}s") {
//available imports
ext{
event = relativePath('src/main/scripts/events.csv')
member = relativePath('src/main/scripts/member.csv')
membership = relativePath('src/main/scripts/membership.csv')
}
//check the target is set
doFirst {
println "\nBulk import of $target\n"
}
main = 'org.yajug.users.bulkimport.BulkImport'
classpath = sourceSets.main.runtimeClasspath
args target
args ext[target]
debug false
}
bulk.dependsOn("bulk${target}")
}