Search

Dark theme | Light theme

October 20, 2010

Gradle Goodness: Display Available Tasks

To see which tasks are available for our build we can run Gradle with the command-line option -t or --tasks. Gradle outputs the available tasks from our build script. By default only the tasks which are dependencies on other tasks are shown. To see all tasks we must add the command-line option --all.

3.times { counter ->
    task "lib$counter" {
        description = "Build lib$counter"
        if (counter > 0) {
            dependsOn = ["lib${counter - 1}"]
        }
    }
} 

task compile {
    dependsOn { 
        project.tasks.findAll { 
            it.name.startsWith('lib')
        }
    }
    description = "Compile sources"
}
$ gradle -q -t

------------------------------------------------------------
Root Project
------------------------------------------------------------

Tasks
-----
:compile - Compile sources
$ gradle -q --tasks -all

------------------------------------------------------------
Root Project
------------------------------------------------------------

Tasks
-----
:compile - Compile sources
    :lib0 - Build lib0
    :lib1 - Build lib1
    :lib2 - Build lib2

But if we add our tasks to a group, we get even more verbose output. Gradle will group the tasks together and without the --all option we get to see all tasks belonging to the group, even those that are dependency tasks. And with the --all option we see for each task on which tasks it depends on. So by setting the group property on the task we get much better output when we ask Gradle about the available tasks.

3.times { counter ->
    task "lib$counter" {
        description = "Build lib$counter"
        if (counter > 0) {
            dependsOn = ["lib${counter - 1}"]
        }
    }
} 

task compile {
    dependsOn { 
        project.tasks.findAll { 
            it.name.startsWith('lib')
        }
    }
    description = "Compile sources"
}

tasks*.group = 'Compile'
$ gradle -q -t

------------------------------------------------------------
Root Project
------------------------------------------------------------

Compile tasks
-------------
:compile - Compile sources
:lib0 - Build lib0
:lib1 - Build lib1
:lib2 - Build lib2
$ gradle -q --tasks -all

------------------------------------------------------------
Root Project
------------------------------------------------------------

Compile tasks
-------------
:compile - Compile sources [:lib0, :lib1, :lib2]
:lib0 - Build lib0
:lib1 - Build lib1 [:lib0]
:lib2 - Build lib2 [:lib1]

Written with Gradle 0.9.