How to display custom task in Gradle Task view

In Eclipse you can use the Gradle Buildship tooling to create a gradle projects and perform it’s tasks. These task could be triggered by the ‘Gradle Task’ view. The view shows all gradle projects, which could be expand to see the task ordered in groups. But why I don’t see my custom tasks in this view?

Create an custom task

To add an task to your project you have to edit the build.gradle file. For example you can add this task, which does nothing:

task foo() {
// do something
}

Save your change, refresh the Gradle Task view and you can see nothing is changed!

Why the view did not show the custom task?

The architecture of gradle decided to make all task, which are not grouped to private. This means a task will not be visible in the Gradle Task view, while you don’t add your task to a group. I found this comment in the gradle discuss forum from a core developer:

It is a general convention of the TAPI that public tasks are those that belong to a non-null group. Any changes on the concept of task visibility (both in the TAPI and on the cmd line) first need to be designed and decided on in Gradle core (and the TAPI would then be adjusted accordingly).

Also the description of the group property gives a hint to this behaviour:

The task group is used in reports and user interfaces to group related tasks together when presenting a list of tasks to the user.

How to display custom task?

To display the task in the Gradle Task view you have to define a group for it.  So open the build.gradle file and add the group property to the task:

task foo() {
// do something
}

// Add the custom task to a group
// to make the task visible in the task view
foo.group = 'foo group'

Save your change, refresh the Gradle Task view and you can see the custom task under the foo group!

Sources

chevron_left
chevron_right

Leave a comment

Your email address will not be published. Required fields are marked *

Comment
Name
Email
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.