November 12, 2009

Gradle Goodness: Execute Tasks with Abbreviations

Gradle supports task name abbreviation to execute tasks. This means we only have to type the portion of a task name that uniquely identifies the task within the project. So we don't have to type the complete name if we can use a shorter version. As a bonus Gradle also supports camel case to abbreviate a task name.

Suppose we have the following build file for our project:

// File: build.gradle
task helloWorld << { ant.echo 'Hello world.' }

The following statements will all invoke this task:

$ gradle helloWorld
$ gradle hello
$ gradle h
$ gradle hW

If the abbreviation is not unique for the project we get a warning from Gradle. For example if we have the tasks hello, helloTask and helloWorld and execute $ gradle h we get a failure message:

FAILURE: Could not determine which task to execute.

* What went wrong:
Task 'h' is ambigious in root project 'project'. Candidates are: 'hello', 'helloWorld', 'helloTask'.

* Try:
Run with -t to get a list of available tasks.