如何使用Gradle运行构建
https://www.flypeng.com win10系统 发布时间:2016-11-06 11:32 来源:未知

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

执行多个任务

Gradle可以从单个构建文件执行多个任务。使用gradle命令处理构建文件。此命令将按列出的顺序编译每个任务,并使用不同的选项执行每个任务以及依赖关系。

示例 - 假设有四个任务 - task1,task2,task3和task4。task3和task4取决于task1和task2。 看看下面的图表。

在上面的四个任务是相互依赖的,用一个箭头符号表示。 看看下面的代码。 将其复制并粘贴到build.gradle文件中。

task task1 << {
   println 'compiling source #1'
}

task task2(dependsOn: task1) << {
   println 'compiling unit tests #2'
}

task task3(dependsOn: [task1, task2]) << {
   println 'running unit tests #3'
}

task task4(dependsOn: [task1, task3]) << {
   println 'building the distribution #4'
}

使用以下代码来编译和执行上述任务。如果命令执行成功,将获得以下输出 -

D:/worksp/flypeng.com/gradle-3.1/study/script>gradle task4 task1
:task1
compiling source #1
:task2
compiling unit tests #2
:task3
running unit tests #3
:task4
building the distribution #4

BUILD SUCCESSFUL

Total time: 1.265 secs

排除任务

要执行中排除某个任务时,可以在gradle命令中使用-x选项,并指出要排除的任务的名称。
使用以下命令用于从上面的脚本中排除 task1 这个任务。
使用以下代码来编译和执行上述任务。如果命令执行成功,将获得以下输出 -

D:/worksp/flypeng.com/gradle-3.1/study/script>gradle task4 -x task1
:task2
compiling unit tests #2
:task3
running unit tests #3
:task4
building the distribution #4

BUILD SUCCESSFUL

Total time: 1.272 secs

发生故障时继续构建

Gradle将在任何任务失败时立即中止执行。但是有时我们希望即使发生故障,也可以继续执行。 为此,要在gradle命令使用-continue选项。它分别处理每个任务及其依赖关系。 重要的是,它将捕获每个遇到的故障,并在构建的执行结束时生成报告。 假设一个任务失败,那么相关的后续依懒任务也不会被执行。

选择执行哪些构建

当运行gradle命令时,它在当前目录中查找构建文件。我们也可以使用-b选项选择指定的构建文件的路径。以下示例显示在subdir/ 子目录中创建一个新文件 newbuild.gradle,并创建一个名称为hello 项目。创建的newbuild.gradle文件的代码内容如下 -

task hello << {
   println "Use File:$buildFile.name in '$buildFile.parentFile.name'."
}

使用以下代码来编译和执行上述任务。如果命令执行成功,将获得以下输出 -

D:/worksp/flypeng.com/gradle-3.1/study/script> gradle -q -b subdir/newbuild.gradle hello
Use File:newbuild.gradle in 'subdir'.

获取构建信息

Gradle提供了几个内置任务来检索有关任务和项目的详细信息。这对理解构建的结构和依赖性以及调试一些问题很有用。可使用项目报告插件向项目中添加任务,来生成这些报告。

列出项目

可以使用gradle -q projects命令来列出所选项目及其子项目的项目层次结构。下面是一个列出构建文件中的所有项目的示例 -

D:/worksp/flypeng.com/gradle-3.1/study/script>gradle -q projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'script'
No sub-projects

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :tasks

报告显示每个项目的描述(如果有指定的话)。可以使用以下命令指定描述。将其粘贴到build.gradle文件中。
再一次执行命令,得到以下结果 -

D:/worksp/flypeng.com/gradle-3.1/study/script>gradle -q projects

------------------------------------------------------------
Root project - The shared API for the application
------------------------------------------------------------

Root project 'script' - The shared API for the application
No sub-projects

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :tasks

列出任务

使用以下命令列出属于多个项目的所有任务。如下所示 -

D:/worksp/flypeng.com/gradle-3.1/study/script>gradle -q tasks --all

------------------------------------------------------------
All tasks runnable from root project - The shared API for the application
------------------------------------------------------------

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 'script'.
components - Displays the components produced by root project 'script'. [incubating]
dependencies - Displays all dependencies declared in root project 'script'.
dependencyInsight - Displays the insight into a specific dependency in root project 'script'.
help - Displays a help message.
model - Displays the configuration model of root project 'script'. [incubating]
projects - Displays the sub-projects of root project 'script'.
properties - Displays the properties of root project 'script'.
tasks - Displays the tasks runnable from root project 'script'.

Other tasks
-----------
task4
    task1
    task2
    task3

以下是其它一些命令及其说明的列表。

编号 命令 描述
1 gradle –q help –task 提供有关指定任务或多个任务的使用信息(如路径,类型,描述,组)。
2 gradle –q dependencies 提供所选项目的依赖关系的列表。
3 gradle -q api:dependencies —configuration 提供有关配置的有限依赖项的列表。
4 gradle –q buildEnvironment 提供构建脚本依赖项的列表
5 gradle –q dependencyInsight 提供了一个洞察到一个特定的依赖
6 gradle –q properties 提供所选项目的属性列表

 
如果你有好的win10资讯或者win10教程,以及win10相关的问题想要获得win10系统下载的关注与报道。
欢迎加入发送邮件到#qq.com(#替换为@)。期待你的好消息!