Gradle - 运行构建
Gradle 提供命令行来执行构建脚本。它可以一次执行多个任务。本章解释如何使用不同的选项执行多个任务。
执行多个任务
您可以从单个构建文件中执行多个任务。Gradle 可以使用gradle 命令处理构建文件。此命令将按照列出的顺序编译每个任务,并使用不同的选项执行每个任务及其依赖项。
示例
有四个任务:task1、task2、task3 和 task4。Task3 和 task4 依赖于 task1 和 task2。请查看下图。
上图中,4 个任务彼此依赖,用箭头符号表示。请查看以下代码。您可以将其复制并粘贴到build.gradle 文件中。
task task1 << { println 'compiling source' } task task2(dependsOn: task1) << { println 'compiling unit tests' } task task3(dependsOn: [task1, task2]) << { println 'running unit tests' } task task4(dependsOn: [task1, task3]) << { println 'building the distribution' }
您可以使用以下代码编译和执行上述任务。
C:\> gradle task4 test
输出
输出如下所示:
:task1 compiling source :task2 compiling unit tests :task3 running unit tests :task4 building the distribution BUILD SUCCESSFUL Total time: 1 secs
排除任务
在排除执行中的任务时,您可以将 –x 选项与 Gradle 命令一起使用,并提及要排除的任务的名称。
使用以下命令从上述脚本中排除 task4。
C:\> gradle task4 -x test
输出
以下是代码的输出:
:task1 compiling source :task4 building the distribution BUILD SUCCESSFUL Total time: 1 secs
继续构建
只要任何任务失败,Gradle 就会中止执行并使构建失败。即使发生故障,您也可以继续执行。为此,您必须将 –continue 选项与 gradle 命令一起使用。它分别处理每个任务及其依赖项。
要点是,它将捕获遇到的每个错误,并在构建执行结束时报告。假设如果一个任务失败,则后续的依赖任务也不会执行。
选择要执行的构建
运行 gradle 命令时,它会在当前目录中查找构建文件。您可以使用 –b 选项选择特定的构建文件以及绝对路径。
以下示例从位于subdir/中的myproject.gradle 文件中选择项目 hello。
task hello << { println "using build file '$buildFile.name' in '$buildFile.parentFile.name'." }
您可以使用以下命令执行上述脚本。
C:\> gradle -q -b subdir/myproject.gradle hello
输出
这将产生以下输出:
using build file 'myproject.gradle' in 'subdir'.
获取构建信息
Gradle 提供多个内置任务来检索有关任务和项目的详细信息。这对于理解构建的结构、依赖项以及调试问题非常有用。
您可以使用 project report 插件向您的项目添加任务,这些任务将生成这些报告。
列出项目
您可以使用gradle –q projects 命令列出所选项目及其子项目的项目层次结构。使用以下命令列出构建文件中的所有项目。这是一个示例:
C:\> gradle -q projects
输出
输出如下所示:
------------------------------------------------------------ Root project ------------------------------------------------------------ Root project 'projectReports' +--- Project ':api' - The shared API for the application \--- Project ':webapp' - The Web application implementation To see a list of the tasks of a project, run gradle <project-path>:tasks For example, try running gradle :api:tasks
如果指定了项目说明,则报告会显示每个项目的说明。您可以使用以下命令指定说明。将其粘贴到build.gradle 文件中。
description = 'The shared API for the application'
列出任务
您可以使用以下命令列出属于多个项目的所有任务。
C:\> gradle -q tasks
输出
输出如下:
------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Default tasks: dists Build tasks ----------- clean - Deletes the build directory (build) dists - Builds the distribution libs - Builds the JAR Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'. components - Displays the components produced by root project 'projectReports'. [incubating] dependencies - Displays all dependencies declared in root project 'projectReports'. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'. help - Displays a help message. model - Displays the configuration model of root project 'projectReports'. [incubating] projects - Displays the sub-projects of root project 'projectReports'. properties - Displays the properties of root project 'projectReports'. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects). To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task>
您可以使用以下命令显示所有任务的信息。
C:\> gradle -q tasks --all
输出
执行上述代码时,您应该看到以下输出:
------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Default tasks: dists Build tasks ----------- clean - Deletes the build directory (build) api:clean - Deletes the build directory (build) webapp:clean - Deletes the build directory (build) dists - Builds the distribution [api:libs, webapp:libs] docs - Builds the documentation api:libs - Builds the JAR api:compile - Compiles the source files webapp:libs - Builds the JAR [api:libs] webapp:compile - Compiles the source files Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'. api:buildEnvironment - Displays all buildscript dependencies declared in project ':api'. webapp:buildEnvironment - Displays all buildscript dependencies declared in project ':webapp'. components - Displays the components produced by root project 'projectReports'. [incubating] api:components - Displays the components produced by project ':api'. [incubating] webapp:components - Displays the components produced by project ':webapp'. [incubating] dependencies - Displays all dependencies declared in root project 'projectReports'. api:dependencies - Displays all dependencies declared in project ':api'. webapp:dependencies - Displays all dependencies declared in project ':webapp'. dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'. api:dependencyInsight - Displays the insight into a specific dependency in project ':api'. webapp:dependencyInsight - Displays the insight into a specific dependency in project ':webapp'. help - Displays a help message. api:help - Displays a help message. webapp:help - Displays a help message. model - Displays the configuration model of root project 'projectReports'. [incubating] api:model - Displays the configuration model of project ':api'. [incubating] webapp:model - Displays the configuration model of project ':webapp'. [incubating] projects - Displays the sub-projects of root project 'projectReports'. api:projects - Displays the sub-projects of project ':api'. webapp:projects - Displays the sub-projects of project ':webapp'. properties - Displays the properties of root project 'projectReports'. api:properties - Displays the properties of project ':api'. webapp:properties - Displays the properties of project ':webapp'. tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects). api:tasks - Displays the tasks runnable from project ':api'. webapp:tasks - Displays the tasks runnable from project ':webapp'.
以下是命令列表及其说明。
序号 | 命令 | 说明 |
---|---|---|
1 | gradle –q help –task <task name> | 提供有关特定任务或多个任务的用法信息(例如路径、类型、说明、组)。 |
2 | gradle –q dependencies | 提供所选项目的依赖项列表。 |
3 | gradle -q api:dependencies --configuration <task name> | 提供关于配置的有限依赖项列表。 |
4 | gradle –q buildEnvironment | 提供构建脚本依赖项的列表。 |
5 | gradle –q dependencyInsight | 提供对特定依赖项的深入了解。 |
6 | Gradle –q properties | 提供所选项目的属性列表。 |