📜  Gradle-运行构建

📅  最后修改于: 2020-10-16 02:43:10             🧑  作者: Mango


Gradle提供了一个命令行来执行构建脚本。它一次可以执行多个任务。本章介绍如何使用不同的选项执行多个任务。

执行多个任务

您可以从一个构建文件中执行多个任务。 Gradle可以使用gradle命令处理该构建文件。该命令将按照列出的顺序编译每个任务,并使用不同的选项执行每个任务以及相关性。

示例-有四个任务-task1,task2,task3和task4。 Task3和Task4取决于任务1和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将中止执行,并在任何任务失败后立即使构建失败。即使发生故障,您也可以继续执行。为此,您必须在gradle命令中使用–continue选项。它分别处理每个任务及其依赖性。最重要的一点是它将捕获每个遇到的故障并在构建执行结束时报告。假设如果某个任务失败,那么从属的后续任务也将不会执行。

选择要执行的构建

当您运行gradle命令时,它将在当前目录中查找构建文件。您可以使用–b选项选择特定的构建文件以及绝对路径。下面的示例从位于subdir /中的myproject.gradle文件中选择项目问候。

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提供了一些内置任务,用于检索有关任务和项目的信息详细信息。这对于了解构建的结构和依赖性以及调试问题很有用。您可以使用项目报告插件将任务添加到项目中,这将生成这些报告。

上市项目

您可以使用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 :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 

您可以使用以下命令显示所有任务的信息。

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'.

这是表中的一些命令列表,描述了不同的选项。

Sr. No. Command Description
1 gradle –q help –task Provides the usage information (such as path, type, description, group) about a specific task or multiple tasks.
2 gradle –q dependencies Provides a list of dependencies of the selected project.
3 gradle -q api:dependencies –configuration Provides the list of limited dependencies respective to configuration.
4 gradle –q buildEnvironment Provides the list of build script dependencies.
5 gradle –q dependencyInsight Provides an insight into a particular dependency.
6 Gradle –q properties Provides the list of properties of the selected project.