Gradle Aggregation Task
In my gradle scripts, I created a task that starts the java process. This process depends on the target property. The task is defined as follows:
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 } } }
And to run:
gradle bulk -Ptarget=event
It works fine, but I know I need to start this process for different purposes:
gradle bulk -Ptarget=event gradle bulk -Ptarget=member gradle bulk -Ptarget=membership ...
How can I group all these calls into another separate task using gradle dependency model? (I know the target list)
Decision
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}") }
source to share
How can I group all these calls into another separate task using gradle dependency model?
You can't, because the task (instance) will run at most once per assembly. Instead, the way is to declare multiple task instances. You can do this by placing a task declaration in a loop, placing it in a method and calling it multiple times, or by writing a task class and creating it multiple times. Then you add another task that depends on all tasks bulk
. Assuming the order of execution between tasks bulk
doesn't matter what it is.
By adding a helper method or two, you can create a nice little API around this to improve readability and allow for reuse elsewhere.
Another way to deal with this is with the task rule. You can read more about task rules in the Gradle User Guide .
source to share
How about build -Ptarget=all
, and look at that specific case in build.gradle file. You can do it like this:
['event','member','membership'].each { t ->
task("bulk${t}", ...) {
onlyIf project.hasProperty("target") && (project.getProperty("target").equals(t) || project.getProperty("target").equals("all"))
args target
args bulk.ext[target]
debug false
}
}
source to share