Part3-学习使用gradle命令行

这一章节主要介绍gradle命令行的基础,我们使用gradle开头的命令来运行一个构建任务

我们可以一次性运行多个gradle任务,使用空格做为每个任务之间的分隔符,比如:命令 gradle compile运行任务compile,compile test运行任务compile和任务test,其中compile和test按顺序执行(执行顺序是:compile的依赖任务-compile-test的依赖任务-test)。

注意:每个任务仅仅会被执行一次,下面举个🌰说明一下:

定义了四个任务,dist、test、compileTest、compile


commandLineTutorialTasks.png

依赖关系如箭头所示。dist依赖test和compile任务,test依赖compileTest和compile任务,compileTest又依赖compile任务。

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 dist test后的输出:

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

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed

执行顺序是compile compileTest test dist
每个任务仅仅执行了一次,所以gradle test test和gradle的效果是一样的

假如不需要执行test任务,则可以使用-x选项进行排除
执行gradle dist -x test后的输出

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

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

可以看出test任务没有被执行,test的依赖任务compileTest也没有执行。

默认情况下,gradle遇到执行出错的任务会停止执行,这样构建任务可以快速的停止,并且抛出异常。但是这样会导致未被执行到的任务的错误不能被发现。为了尽可能发现更多的错误,可以使用 --continue 选项。

当使用 --continue 选项的时候,gradle执行时遇到错误会继续执行下一个任务,并且在执行完成时在命令行末尾抛出所有错误。

如果一个任务执行失败了,那么依赖(直接或间接)这个任务的其他任务将不会被执行。

task任务的名称支持缩略形式,我们只需要提供足够的特征字符就可以。比如上边的例子,我们可以使用 gradle d 命令执行dist任务

执行gradle di命令

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

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed

也可以依照驼峰命名规则来书写缩略名称,gradle compileTest =
gradle compTest = gradle cT

gradle cT的输出

> gradle cT
:compile
compiling source
:compileTest
compiling unit tests

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

使用 -x(排除) 选项时依然可以使用缩写形式。

当我们使用gradle命令时,会在当前目录下寻找build文件,我们可以使用-b选项选择其他目录下的build文件

比如说:存在文件subdir/myproject.gradle

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

执行gradle -q -b subdir/myproject.gradle hello

> gradle -q -b subdir/myproject.gradle hello
using build file 'myproject.gradle' in 'subdir'.

或者我们也可以使用 -p 选项来指定整个项目目录,多项目目录使用 -p 代替 -b 选项

gradle -q -p subdir hello

> gradle -q -p subdir hello
using build file 'build.gradle' in 'subdir'.

-q 参数的作用是什么?
该文档的示例中很多地方在调用 gradle 命令时都加了 -q 参数。该参数用来控制 gradle 的日志级别,可以保证只输出我们需要的内容。

日志级别:
ERROR 错误消息
QUIET 重要的信息消息
WARNING 警告消息
LIFECYCLE 进度信息消息
INFO 信息性消息
DEBUG 调试消息

日志参数:
没有日志选项 LIFECYCLE 及更高
-q or –quiet QUIET 及更高
-i or –info INFO 及更高
-d or –debug DEBUG 及更高
-s or –stacktrace 打印关键堆栈信息
-S or –full-stacktrace 打印全栈信息

gradle内置的任务一般都是支持增量构建的,任务执行与否,取决于任务的input和output是否存在修改。如果任务不需要执行则出现 UP-TO-DATE 标识

gradle doIt

> gradle doIt
:doIt UP-TO-DATE

gradle --rerun-tasks doIt

> gradle --rerun-tasks doIt
:doIt

这个 --rerun-tasks 选项会强制执行所有任务(当前任务依赖的任务),和 clean 命令有些类似,但是不会清除构建产出。

运行 gradle projects 会以分层的形式打印出当前project的所有子project

gradle -q projects

> 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

可以看到打印出了子project名字和描述信息,描述信息是可以通过description属性进行设置的

比如在build.gradle文件中:

description = 'The shared API for the application'

运行命令 gradle tasks 会列出当前project的主要tasks和每个task的description

gradle -q tasks

> 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.
wrapper - Generates Gradle wrapper files.

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'.
dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating]
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>

仅仅是展示了分组下的task(包括子project的task),称作可见task,我们可以给task指定group属性,当然和project类似,也存在description属性。

build.gradle

dists {
    description = 'Builds the distribution'
    group = 'build'
}

使用 --all 选项,可以查看所有的task

gradle -q tasks --all

> 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 - Builds the JAR
webapp:libs - Builds the JAR

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

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'.
dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating]
api:dependentComponents - Displays the dependent components of components in project ':api'. [incubating]
webapp:dependentComponents - Displays the dependent components of components in project ':webapp'. [incubating]
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'.

Other tasks
-----------
api:compile - Compiles the source files
webapp:compile - Compiles the source files
docs - Builds the documentation
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容