Tech and Media Labs
This site uses cookies to improve the user experience.




Run Gradle

Jakob Jenkov
Last update: 2015-08-10

To run Gradle from the command line you must first have installed Gradle correctly. When installed correctly you can run Gradle using this command line:

gradle

The gradle command will run Gradle on the gradle build script located in the same directory as the command prompt is located in. That means, that to run gradle on a specific gradle build script you must change directory in the command prompt into the directory where the build script is located.

Run a Task

The Gradle build script typically contains one or more tasks which you can execute. You execute a task in a build script by listing the task name after the gradle command on the command line. Here is how to run a Gradle task:

gradle compileCode

This command runs the task named compileCode in the build script located in the same directory as the command is executed from.

Run Multiple Tasks

Gradle can run multiple tasks with a single command by providing the names of all the tasks to run. The names of the tasks to run should be separated by a space. Here is an example of a Gradle command running multiple tasks:

gradle clean build

This command line will make Gradle first execute the task named clean and then the task named build . Gradle will also execute the tasks the named tasks depends on (if any).

Each task will only get executed once, regardless of how many times it is found as part of the listed tasks or the tasks the listed tasks depend on. Thus, this command will only execute the clean task once:

gradle clean clean

Task Name Abbreviation

You don't actually have to write the full task name of a task in order to execute it. Gradle just need enough of the task name to be able to distinguish it uniquely from other tasks in your Gradle build script. For example you can run the build task using this command:

gradle b

A b is enough to uniquely identify the task build in a Gradle build script if no other tasks start with the letter b. If other tasks start with a b too, you may have to provide more of the task name. These Gradle command examples show how that could look:

gradle bu
gradle bui
gradle buil

Excluding Tasks

You can exclude tasks from execution using the -x flag. Excluding tasks is mostly useful to exclude a task which another task depends on, typically to speed up the build process by excluding tasks you know you don't need to execute.

For example, the build task from the Gradle Java plugin depends on the tasks test and testClasses . Perhaps you have just executed the tests and know that they succeed. Therefore you do not want to run the test and testClasses tasks again, but only build the project. You can do so with this Gradle command:

gradle build -x test

This will exclude the test task from execution. Since the test task depends on the testClasses task and the test task is now excluded, the testClasses is also implicitly excluded.

Quiet Mode

You can run gradle in a "quiet mode" which means that the Gradle command leaves out a lot of the status messages it normally prints to the command line when executing. To run Gradle in quiet mode you add the command line switch -q to the command line. Here is an example showing how to run Gradle in quiet mode:

gradle -q

Or if you want to run a specific task:

gradle -q compileCode

Listing Tasks in Build Script

You can list all tasks in a build script by passing the command line argument tasks to the gradle command, like this:

gradle tasks

This will list all the tasks found in the build script located in the directory in which you run this command.

If you use the --all flag you will get more information about each task. Here is an command line example:

gradle tasks --all

Specifying Build Script

By default Gradle executes the build script located in the same directory in which the gradle command is executed. However, it is possible to choose to build another build script.

You specify another build script using the -b command line flag. Here is an example:

gradle -b subproject-dir/build.gradle build

This example Gradle command builds the build script located in the subproject-dir directory, and runs the build task of that build script.

When you specify another build script Gradle does not use its settings.gradle file.

Specifying Project

As an alternative to specifying a different build script than the one found at the default location, you can also specify another project to build. A project is identified by the directory in which the project is located.

You run the build script of another project by using the -p flag followed by the directory of the project. Here is an example of specifying what project to build:

gradle -p subproject-dir build

This example will build the project found in the directory subproject-dir. Gradle will use the build.gradle file found in the project directory (subproject-dir) and will execute the build task in that build script.

Listing Subprojects

Gradle can list all subprojects of a project (as specified inside the Gradle build script for the project). You do so using the projects task. Here is how that looks:

gradle projects

Gradle Help

You can get a list of the gradle command's options by passing the -h flag to the gradle command, like this:

gradle -h

Build Failures

If a task in the build script fails during execution, Gradle will abort the whole build. This is done to save you time. Often, later tasks in the build script make no sense to execute if earlier tasks fail.

You can instruct Gradle to continue the build even if a task fails. This is done using the --continue flag, like this:

gradle build --continue

Gradle will continue executing all tasks where the tasks it depends on were executed successfully. Thus, if task B depends on task A and A fails, then Gradle will actually not execute B, even if you instruct Gradle to continue the build despite errors.

Dry Run

A "dry run" is a run where Gradle does not actually execute all the tasks you tell it to execute. Instead Gradle prints out information about what tasks that would have been executed in case you had run Gradle normally.

You signal to Gradle to make a dry run using the -m flag. Here is a Gradle dry run command example:

gradle -m build

The output from a dry run will look somewhat similar to this:

D:\data\projects\gradle-experiments>gradle -m build
:compileJava SKIPPED
:processResources SKIPPED
:classes SKIPPED
:jar SKIPPED
:assemble SKIPPED
:compileTestJava SKIPPED
:processTestResources SKIPPED
:testClasses SKIPPED
:test SKIPPED
:check SKIPPED
:build SKIPPED

BUILD SUCCESSFUL

Total time: 14.869 secs
D:\data\projects\gradle-experiments>

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC