Command Line
1 Executing multiple tasks
gradle compile test
上述命令将会执行compile和test任务. 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 how it came to be included in the build: whether it was specified on the command-line, or as a dependency of another task, or both.
使用命令 gradle dist test 执行多个任务
文件 build.gradle:
task compile {
doLast {
println 'compiling source'
}
}
task compileTest(dependsOn: compile) {
doLast {
println 'compiling unit tests'
}
}
task test(dependsOn: [compile, compileTest]) {
doLast {
println 'running unit tests'
}
}
task dist(dependsOn: [compile, test]) {
doLast {
println 'building the distribution'
}
}
2 Excluding tasks
不执行 test 任务:
gradle dist -x test
3 Continuing the build when a failure occurs
默认情况下,Gradle will abort execution and fail the build as soon as any task fails. 但是也会隐藏其它即将遇到的错误. In order to discover as many failures as possible in a single build execution, you can use the --continue option.
gradle --continue
When executed with --continue, Gradle will execute every task to be executed where all of the dependencies for that task completed without failure, instead of stopping as soon as the first failure is encountered. Each of the encountered failures will be reported at the end of the build.
4 Task name abbreviation (缩写)
You only need to provide enough of the task name to uniquely identify the task. You can also abbreviate each word in a camel case task name. For example, you can execute task compileTest by running gradle compTest or even gradle cT. You can also use these abbreviations with the -x command-line option.
gradle di
gradle cT
gradle compTest
5 Selecting which build to execute
When you run the gradle command, it looks for a build file in the current directory (首先在当前目录查找 build 文件). You can use the -b option to select another build file (选择一个其它 build 文件).
文件 subdir/myproject.gradle:
task hello {
doLast {
println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
}
}
使用命令:
gradle -q -b subdir/myproject.gradle hello
-q, --quiet: Log errors only
Alternatively, you can use the -p option to specify the project directory (指定项目路径-目录) to use. For multi-project builds you should use -p option instead of -b option.
gradle -q -p subdir hello
6 Forcing tasks to execute
Many tasks, particularly those provided by Gradle itself, support incremental builds. Such tasks can determine whether they need to run or not based on whether their inputs or outputs have changed since the last time they ran. You can easily identify tasks that take part in incremental build when Gradle displays the text UP-TO-DATE next to their name during a build run.
You may on occasion want to force Gradle to run all the tasks, ignoring any up-to-date checks. If that's the case, simply use the --rerun-tasks option. Here's the output when running a task both without and with --rerun-tasks:
gradle --rerun-tasks doIt
Note that this will force all required tasks to execute, not just the ones you specify on the command line. It's a little like running a clean, but without the build's generated output being deleted.
7 Obtaining information about your build
Gradle provides several built-in tasks which show particular details of your build. This can be useful for understanding the structure and dependencies of your build, and for debugging problems.
7.1 Listing projects
Running gradle projects gives you a list of the sub-projects of the selected project, displayed in a hierarchy.
gradle projects
gradle -q projects
The report shows the description of each project, if specified. You can provide a description for a project by setting the description property:
description = 'The shared API for the application'
7.2 Listing tasks
Running gradle tasks gives you a list of the main tasks of the selected project.
gradle tasks
gradle -q tasks
By default, this report shows only those tasks which have been assigned to a task group, so-called visible tasks. You can do this by setting the group property for the task. You can also set the description property, to provide a description to be included in the report.
改变 task report:
dists {
description = 'Builds the distribution'
group = 'build'
}

You can obtain more information in the task listing using the --all option. With this option, the task report lists all tasks in the project, including tasks which have not been assigned to a task group (没有 group 的 task 也会被列举出来), so-called hidden tasks. Here is an example:
gradle -q tasks --all

7.3 Show task usage details
Running gradle help --task someTask gives you detailed information about a specific task or multiple tasks matching the given task name in your multi-project build. Below is an example of this detailed information:
gradle -q help --task libs
7.4 Listing project dependencies
Running gradle dependencies gives you a list of the dependencies of the selected project, broken down by configuration. For each configuration, the direct and transitive dependencies of that configuration are shown in a tree. Below is an example of this report:
gradle -q dependencies api:dependencies webapp:dependencies
Since a dependency report can get large, it can be useful to restrict the report to a particular configuration. This is achieved with the optional --configuration parameter:
gradle -q api:dependencies --configuration testCompile
7.5 Listing project buildscript dependencies
Running gradle buildEnvironment visualises the buildscript dependencies of the selected project, similarly to how gradle dependencies visualises the dependencies of the software being built.
7.6 Getting the insight into a particular dependency
Running gradle dependencyInsight gives you an insight into a particular dependency (or dependencies) that match specified input. Below is an example of this report:
gradle -q webapp:dependencyInsight --dependency groovy --configuration compile
7.7 Listing project properties
Running gradle properties gives you a list of the properties of the selected project. This is a snippet from the output:
gradle -q api:properties
7.8 Profiling a build (时间测量)
The --profile command line option will record some useful timing information while your build is running and write a report to the build/reports/profile directory. The report will be named using the time when the build was run.
This report lists summary times and details for both the configuration phase and task execution. The times for configuration and task execution are sorted with the most expensive operations first. The task execution results also indicate if any tasks were skipped (and the reason) or if tasks that were not skipped did no work.
Builds which utilize a buildSrc directory will generate a second profile report for buildSrc in the buildSrc/build directory.
8 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 option for this. For example, if you run “gradle -m clean compile”, you'll see all the tasks that would be executed as part of the clean and compile tasks. This is complementary to the tasks task, which shows you the tasks which are available for execution.