Chapter 9. Using the Gradle Command-Line

This chapter introduces the basics of the Gradle command-line. You run a build using the gradle command, which you have already seen in action in previous chapters.

9.1. Executing multiple tasks

You can execute multiple tasks in a single build by listing each of the tasks on the command-line. For example, the command gradle compile test will execute the compile and test tasks. Gradle will execute the tasks in the order that they are listed on the command-line, and will also execute the dependencies for each task. Each task is executed once only, regardless of why it is included in the build: whether it was specified on the command-line, or it a dependency of another task, or both. Let's look at an example.

Below three tasks are defined. Both libs and test depend on compile task. Execution of gradle -q libs test command for this build script results in compile task being executed only once.

Example 9.1. Executing multiple tasks

build.gradle

task compile << {
    println 'compiling source'
}

task test(dependsOn: compile) << {
    println 'running tests'
}

task libs(dependsOn: compile) << {
    println 'building libs'
}

Output of gradle -q libs test

> gradle -q libs test
compiling source
building libs
running tests

Because each task is executed once only, executing gradle libs libs is exactly the same as executing gradle libs.

9.2. Selecting which build to execute

When you run the gradle command, it looks for a build file in the current directory. You can use the -b option to select another build file. For example:

> gradle -b subproject/build.gradle

Alternatively, you can use the -p option to specify the project directory to use:

> gradle -p subproject

9.3. Obtaining information about your build

Gradle provides several command-line options which show particular details of your build. This can be useful for understanding the structure and dependencies of your build, and for debugging problems.

Running gradle --tasks gives you a list of the tasks which make up the build, broken down by project. This report shows the default tasks, if any, of each project, and the description and dependencies of each task. Below is an example of this report:

Example 9.2. Obtaining information about tasks

Output of gradle -q --tasks

> gradle -q --tasks
------------------------------------------------------------
Root Project
------------------------------------------------------------
Default Tasks: dists

:clean - Deletes the build directory (build)
:dists 
   -> :api:libs, :webapp:libs

------------------------------------------------------------
Project :api
------------------------------------------------------------
:api:libs 
rule - build<ConfigurationName>: builds the artifacts of the given configuration

------------------------------------------------------------
Project :webapp
------------------------------------------------------------
:webapp:libs 
rule - build<ConfigurationName>: builds the artifacts of the given configuration

Running gradle --dependencies gives you a list of the dependencies of the build, broken down by project. This report shows the configurations of each project. For each configuration, the direct and transitive dependencies of that configuration are shown. Below is an example of this report:

Example 9.3. Obtaining information about dependencies

Output of gradle -q --dependencies

> gradle -q --dependencies
------------------------------------------------------------
Root Project
------------------------------------------------------------
No configurations

------------------------------------------------------------
Project :api
------------------------------------------------------------
compile
|-----junit:junit:4.4:default

------------------------------------------------------------
Project :webapp
------------------------------------------------------------
compile
|-----commons-io:commons-io:1.2:default

Running gradle --properties gives you a list of the properties of each project in the build.

You can also use the project report plugin to add a number of reporting tasks to your project.

9.4. Dry Run

Sometimes you are interested in which tasks are executed in which order for a given set of tasks specified on the command line, but you don't want the tasks to be executed. You can use the -m for this. For example gradle -m clean compile shows you all tasks to be executed as part of the clean and compile tasks. This is complementary to the -t, which shows you all available tasks for execution.

You can find out more about the gradle command's usage in Appendix B, Gradle Command Line