How can I specify a category for a Gradle task?

17.2k views Asked by At

I am writing a Gradle task in Intellij IDEA. I have noticed that in the Gradle window, the tasks appear under folders like so:

enter image description here

I am wondering, how can you give a task a 'category' so that it appears in a folder as shown in the screenshot?

All the tasks I create usually end up in other. I am also writing a custom plugin and want it to appear under a 'folder' name of my choosing. but I assume it'll be the same answer for when writing a task.

5

There are 5 answers

1
tim_yates On BEST ANSWER

You just need to set the group property of your task. Eg (from http://mrhaki.blogspot.co.uk/2012/06/gradle-goodness-adding-tasks-to.html)

task publish(type: Copy) {
    from "sources"
    into "output"
}

configure(publish) {   
    group = 'Publishing'
    description = 'Publish source code to output directory'
}
0
Andrii Abramov On

Also, nice way to group tasks and avoid boilerplate code is the next:

class PublishCopy extends Copy {
    PenguinTask() {
        group = 'publish copy'
    }
}

And then you don't have to specify task group each time:

task copySources(type: PublishCopy) {
  from "sources"
  into "output"
}

task copyResources(type: PublishCopy) {
  from "res"
  into "output/res"
}
0
Mahozad On

This is for Kotlin DSL (build.gradle.kts scripts):

tasks.create("incrementVersion") {
    group = "versioning"
    // ...
}
0
Oleksandr Bondarchuk On

If you have many tasks, you can configure group as follows:

def groupName = "group-name"
task1.group = groupName
task2.group = groupName
task3.group = groupName
0
Vadym Chekan On

Or, shorter syntax:

task publish(type: Copy) {
  group = "Publishing"
  description = "Publish source code to output directory"
  from "sources"
  into "output"
}