Adding dynamic tasks to group

Hi, so I have a setup where I dynamically create tasks depending on some build variants. If another build variant is added then a task will automatically be created for it.

pseudo code:

for all variants in dir do {
task “$variant”(type:exec, dependsOn:‘unzipDeps’) {
outputs.dir
workingDir
commandline
}
}

Now I would like all these tasks to be added to a group. If I create the tasks statically it is easy with just
taskName.group = “CustomBuild”

But this doesnt seem to work when I do it like this. Even if I create another task which depends on this task generation I just get different errors like the name of the task is a string and the property group doesnt exist and others… My first try was something like this:

for all variants in dir do {
task “$variant”(type:exec, dependsOn:‘unzipDeps’) {
outputs.dir
workingDir
commandline
}
"$variant".group = “CustomTask”
}

But alas no luck… Any one got an idea?

If your code is really structured like that, you might as well set the group as you create the task, not immediately after in the same loop (I’ve modified your pseudo-code to be working, stand-alone code):

ext.variants = ['alpha', 'beta', 'delta']
  
for (def variant in variants) {
    task "$variant"(type: Exec) {
        commandLine 'echo', variant
        group = 'CustomBuild'
    }
}

If there’s some reason you want to keep this out of the task declaration, your original attempt needs to be prefixed with tasks. In the non-dynamic case, you’re accessing a property that’s a task instance. In the dynamic case, you just have a GString that can be resolved, but you can’t get a task instance from that directly. You need to lookup a task with that dynamic name. tasks."$variant" gets you the instance that you can then set a property on.

ext.variants = ['alpha', 'beta', 'delta']
  
for (def variant in variants) {
    task "$variant"(type: Exec) {
        commandLine 'echo', variant
    }
    tasks."$variant".group = 'CustomBuild'
}

Thank you for such a simple solution :slight_smile: