Gradle学习笔记 Gradle命令行

时间:2022-09-12 17:40:53

执行多个任务

安装好Gradle之后,就可以使用Gradle命令行了。首先我们先新建一个文件夹gradle-learn,然后在其中新建一个build.gradle,这是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'
}
}

Gradle执行的基本单元是任务,在上面我们定义了4个任务。这4个任务之间还存在依赖关系,使用dependsOn定义。然后我们使用Gradle运行dist和test任务,会发现Gradle会分析这几个任务之间的依赖关系,依次运行它们,gradle dist test

结果如下。

$ gradle dist test
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution

BUILD SUCCESSFUL

Total time: 2.22 secs

排除任务

可以用-x参数排除某个任务。现在我们运行dist而不运行test:gradle dist -x test。然后查看一下输出,我们会发现输出非常有趣。

$ gradle dist -x test
:compile
compiling source
:dist
building the distribution

BUILD SUCCESSFUL

Total time: 2.277 secs

仔细研究一下就会发现Gradle非常智能,会自动选择最优的执行路线。由于排除了test任务,所以compileTest任务其实也不需要了,因为它仅仅被test依赖。由于compile任务也是dist需要的,所以必须被执行。我们可以看到,Gradle会自动选择最优的执行路线

出错时继续执行

默认情况下如果构建过程出现错误Gradle就会停止构建,我们可以使用--continue参数让Gradle在出现错误的时候继续执行构建过程,直到每个任务都运行完毕。然后Gradle会报告所有错误。这个选项通常用来发现构建过程中的所有错误。

任务的缩写

在运行Gradle的时候我们不用完整输入任务名称,如果任务的前几个字母就可以区分任务,我们就可以只输入这几个字母。比如gradle d相当于gradle dist。另外Gradle还支持驼峰命名法的缩写。比如说我们可以运行gradle cT,相当于gradle compileTest

选择构建文件

默认情况下Gradle会选择当前文件夹下的build.gradle作为构建文件。我们也可以手动使用-b参数指定其他的构建文件。比如新建一个子文件夹subdir并在其中添加一个myproject.gradle文件。

myproject.gradle文件内容如下:

task hello {
doLast {
println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
}
}

然后我们来运行一下,gradle -q -b subdir/myproject.gradle hello。结果如下。

using build file 'myproject.gradle' in 'subdir'.

这里还使用了-q静默参数,功能是只显示任务输出,不显示其他构建过程的输出。对于多个文件夹的构建项目,还可以使用-p参数指定要使用的构建文件的文件夹,例如我们将subdir中的构建文件重命名为build.gradle,然后运行gradle -q -p subdir hello,会得到和上面类似的输出。

强制执行任务

一些任务特别是gradle自带的任务,都支持增量构建。这样的任务会在运行时候根据文件状态自己决定执行与否。如果有这样的任务,在运行的时候会显示一个UP-TO-DATE字样。我们可以通过--rerun-tasks参数强制任务完整执行。

获取构建信息

列出项目

我们可以通过在build.gradle文件中添加description = ...来为项目添加描述信息。比如我们现在向build.gradle添加下面一行:

description = 'This is some info'

如果项目中包含多个子项目的话,可以使用gradle -q projects列出所有项目的信息。结果如下。

$ gradle projects -q

------------------------------------------------------------
Root project - This is some info
------------------------------------------------------------

Root project 'gradle-learn' - This is some info
No sub-projects

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

列出任务

使用gradle -q tasks列出所有任务。结果如下。

$ gradle -q tasks

------------------------------------------------------------
All tasks runnable from root project - This is some info
------------------------------------------------------------


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

Other tasks
-----------

dist

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

我们可以看到dist任务在其他任务中。我们可以通过给任务添加信息来改变这些显示。添加添加任务描述和分组信息,让dist任务变成这样。

task dist(dependsOn: [compile, test]) {
doLast {
println 'building the distribution'
}
description = ' This is a dist info'
group = 'build'
}

然后再次运行gradle -q tasks。会发现这次结果发生了变化。

另外还可以添加--all参数,显示出所有任务信息。

显示任务帮助

使用gradle help --task someTask来显示任务帮助。

显示执行顺序

使用-m参数可以以Dry Run的方式运行Gradle,在这种方式下不会执行任何任务,只会列出这些任务的执行顺序。例如:

$  gradle dist test -m
:compile SKIPPED
:compileTest SKIPPED
:test SKIPPED
:dist SKIPPED

BUILD SUCCESSFUL

Total time: 1.834 secs

还有其他一些命令行参数,由于和具体的项目有关,会在具体情况下说明。这里就不在列出了。

参考资料

https://docs.gradle.org/current/userguide/tutorial_gradle_command_line.html