bss36504
bss36504

Reputation: 133

How to create dynamic gradle tasks based on list of strings

I see this example of dynamic Gradle tasks on various sites:

4.times { counter ->
task "task$counter" << {
    println "I'm task number $counter"
    }
}

I would like to create dynamic tasks based on a list of strings such as:

def taskSuffixes = ["foo", "bar", "baz"]

taskSuffixes.each { it ->
    task t_$it << {
        println "My name is: $it"
        }
    }

However, that does not seem to work. Is this possible? If it is not possible, what would be a good alternative given that my list will be strings and I will eventually need those strings within the matching task?

Upvotes: 1

Views: 1500

Answers (1)

tim_yates
tim_yates

Reputation: 171154

You need to put double quotes round your templated string

task "t_$it" << {
    println "My name is: $it"
}

<< was deprecated around Gradle 4, the new way is to use doLast

task "t_$it" {
    doLast {
        println "My name is: $it"
    }
}

Upvotes: 3

Related Questions