The Gradle command-line interface
Gradle, just like other build tools, is primarily run from a command line. That's why it is worth spending some time to get familiar with its command-line interface. Typically, a gradle
command is issued from the root of a project directory with some tasks to be executed. Let's say we are in the hello-gradle
directory, which is currently empty.
Gradle provides a very simple command-line interface (CLI), which takes the following form:
gradle [options…] [tasks…]
As we can see, apart from the gradle
command itself, everything else is optional. The options
tweak the execution of the Gradle whereas tasks
, which we will see in detail later, are the basic units of work. Options are common across all projects and are specific to Gradle but tasks may vary depending on the project in which the gradle
command is being run.
There are some tasks that are available on all projects. One such task is help
:
$ gradle help :help Welcome to Gradle 2.9. To run a build, run gradle <task> ... To see a list of available tasks, run gradle tasks To see a list of command-line options, run gradle --help To see more detail about a task, run gradle help --task <task> BUILD SUCCESSFUL Total time: 0.639 secs
Gradle is helping us out by telling us how to find all the available tasks and list all command-line options. Let's first check what other tasks are currently available on our project. Remember we are still in the empty directory hello-gradle
:
$ gradle tasks :tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Help tasks ---------- components - Displays the components produced by root project 'hello-gradle'. [incubating] dependencies - Displays all dependencies declared in root project 'hello-gradle'. dependencyInsight - Displays the insight into a specific dependency in root project 'hello-gradle'. help - Displays a help message. model - Displays the configuration model of root project 'hello-gradle'. [incubating] projects - Displays the sub-projects of root project 'hello-gradle'. properties - Displays the properties of root project 'hello-gradle'. tasks - Displays the tasks runnable from root project 'hello-gradle'. To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task> BUILD SUCCESSFUL Total time: 0.652 secs
This shows us some generic tasks that are available even without us adding any task to our project. We can try running all these tasks and see the output. We will see these tasks in details in the upcoming chapters.
The other useful command gradle help
suggested us to check all the available options with the --help
option.
Tip
The help
task is not the same as the --help
option.
When we run the gradle --help
command, we get the following output:
$ gradle --help USAGE: gradle [option...] [task...] -?, -h, --help Shows this help message. -a, --no-rebuild Do not rebuild project dependencies. -b, --build-file Specifies the build file. …..
(The output is truncated for brevity.)
The option has a long form such as --help
and may have a short from such as -h
. We have already used one option before, that is --version
or -v
, which prints information about the Gradle version. The following are some commonly used options; there are many more options, which can be seen using the gradle --help
command:
Options |
Description |
---|---|
|
This specifies a build file (default: |
|
This continues task execution even after a task failure |
|
This sets the system property of the JVM |
|
This prints debug level logs |
|
This starts Gradle GUI |
|
This prints info level logs |
|
This adds a property to the project |
|
This logs only errors |
|
This prints stack traces for exceptions |
|
This excludes a specific task |