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.

view sourceprint?

00.3.times { counter ->

01.task "lib$counter" {

02.description = "Build lib$counter"

03.if (counter > 0) {

04.dependsOn = ["lib${counter - 1}"]

05.}

06.}

07.}

08.

09.task compile {

10.dependsOn {

11.project.tasks.findAll {

12.it.name.startsWith(‘lib‘)

13.}

14.}

15.description = "Compile sources"

16.}

$ 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.

view sourceprint?

00.3.times { counter ->

01.task "lib$counter" {

02.description = "Build lib$counter"

03.if (counter > 0) {

04.dependsOn = ["lib${counter - 1}"]

05.}

06.}

07.}

08.

09.task compile {

10.dependsOn {

11.project.tasks.findAll {

12.it.name.startsWith(‘lib‘)

13.}

14.}

15.description = "Compile sources"

16.}

17.

18.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]

时间: 2024-12-20 11:20:17

Gradle Goodness: Display Available Tasks的相关文章

Gradle Goodness: Group Similar Tasks

In Gradle we can assign a task to a group. Gradle uses the group for example in the output of $ gradle -t to output all the tasks of the same group together. We only have to set the group property with a value and our task belongs to a group. In the

Gradle Goodness: Automatic Clean Tasks

Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with

Gradle Goodness: Continue Build Even with Failed Tasks

If we run a Gradle build and one of the tasks fails, the whole build stops immediately. So we have fast feedback of our build status. If we don't want to this and want Gradle to execute all tasks, even though some might have failed, we use the comman

Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of Gradle is incremental build support. With incremental build support a task is only executed if it is really necessary. For example if a task generate

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before t

Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method

Gradle Goodness: Changing Name of Default Build File

Gradle uses the name build.gradle as the default name for a build file. If we write our build code in a file build.gradle then we don't have to specify the build filename when we run tasks. We can create build files with a different name other than b

Gradle Goodness: Adding Tasks to a Predefined Group

In Gradle we can group related tasks using the group property of a task. We provide the name of our group and if we look at the output of the tasks task we can see our tasks grouped in section with the given name. In the next sample we create a new t

Gradle Goodness: Add Incremental Build Support to Tasks

Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unless it is necessary. We can help Gradle and configure our task so it is ready for an incremental build. Suppose we have a task that generates a file. T