Recommended way to set task group in Groovy DSL

We have 2 ways to set the group of a task, with a subtle difference:

Approach 1

tasks.register('myTask') {
    group 'myGroup'
}

Approach 2

tasks.register('myTask') {
    group = 'myGroup'
}

I’d be tempted to choose the slightly simpler approach 1.

However, that introduces inconsistencies when we think about other task properties which only support the = syntax.

tasks.register('myTask', Zip) {
    group 'myGroup'
    archiveFileName = 'file.zip'
}

Now approach 2 seems nicer since it would allow us to use = consistently.

tasks.register('myTask', Zip) {
    group = 'myGroup'
    archiveFileName = 'file.zip'
}

I can’t find anywhere in the Gradle docs about setting the task group.

Is there a recommended approach? Thanks!

My recommendation is to use =, for no other reason than consistency and readability. I like to set my properties with = so it’s clear I’m not trying to call a method (makes it easier for people to translate between groovy dsl and javadoc IMO).

1 Like

Thanks Chris. Any idea how this is implemented?

The Task API docs indicate you shouldn’t be able to call group("Some group") directly.

Maybe some missing method handling going on somewhere?

The method is dynamically added by some Gradle magic at runtime. If you’ve ever printed out the class names of many objects you’ll see that their classnames are something like Abc_Decorated, where Abc is the original class. These decorated classes fill in some of the dsl blanks.

If you’re interested in the gory details, here’s a place to start. I’m not familiar with this code so that’s about all I can help with.