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 this 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:

Example 9.1.  build.gradle

task compile << {
    println 'compiling source'
}

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

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

Below is the result of executing gradle -q libs test for this build script. Notice that the compile task is executed once, even though it is a dependency of both libs and test.

Example 9.2. 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

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