gradle 用户手册 第4章使用Gradle的命令行

本文介绍了Gradle命令行的基础操作,包括执行多个任务、排除任务、获取构建信息等技巧,帮助开发者更高效地管理和构建项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录:

4.1 执行多个task

4.2 排除task

4.3 出现问题继续build

4.4 task名字的简称

4.5 选择执行哪个build

4.6 强制执行task

4.7 获取build的信息

4.8 空跑

4.9 总结

本章介绍Gradle命令行的基础内容。在第3章我们已经稍微提及了一些,比如需要用gradle 命令来运行一个 build。

 

4.1 执行多个task

大家可以通过将想要执行的多个task 依次放到命令行,然后就可以在一个build中一次执行多个task了。比如说,使用命令:gradle compile test ,就会执行 compile和test这两个task。Gradle会按照她们在命令行中的顺序来依次执行,同时也要执行她们依赖的task。每个task只会执行依次,不管她们是直接在命令行声明,还是作为其他task的依赖来执行,或者两者皆是。下面让我们看一个例子。

下面的例子定义了4个task。dist 和 test 都依赖于compile这个任务,如果在这个build脚本中执行:gradle dist test,那么compile这个task只会执行一次。

 

插图4.1 task依赖

 

 Task dependencies

 

样例4.1 执行多个task

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

 

因为每个task只会执行一次,所以命令:gradle test test 和gradle test 没啥区别。

 

 

4.2排除任务

大家可以通过使用-x这个命令行选项,并在其后提供一下task的名字,就可以在运行时把这个task排除掉。下面我们继续用刚刚的那个build文件来试试这个命令。

 

样例 4.2 排除task

命令: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这个task是dist所依赖的,但是在执行dist的时候test是没有被执行的。细心的朋友会发现,就连test的依赖task,比如compileTest也没有被执行,而那些既被test依赖,也被其他task依赖的task,是仍然会被执行的。

 

4.3 出现问题时继续build

 

默认情况下,在任何task执行时出现问题,Gradle都会终止build。这样会使build很快结束,但是可能隐藏其他问题。为了能够在一次build中找出更多的隐患,大家可以使用 --continue这个选项。

如果使用了 --continue这个命令选项,Gradle会执行所有其依赖task没有出错的task。比如说,如果test依赖的compilation(无论直接依赖还是间接依赖)这个task在执行时出现问题,那么即使使用了--conitnue 这个选项,test这个task依然不会执行。

 

4.4 task简称

当在命令行执行一个task的时候,我们不需要提供这个task的全称。只需提供一个能够唯一确定这个task的简称就行了。比如,在上面的那个build脚本中,如果要执行dist这个task,只需要运行:gradle d这个命令就可以了。

 

案例 4.3 简写task名字

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

 

当然也可以使用驼峰式的简写。比如要执行compileTest这个task,运行命令:gradle comTest或者gradle cT都可以

 

案例4.4 安装驼峰简写task名字

gradle cT的输出:

> gradle cT

:compile

compiling source

:compileTest

compiling unit tests

 

BUILD SUCCESSFUL in 0s

2 actionable tasks: 2 executed

大家也可以通过 -x 这个命令选项来使用task简称。

 
4.5 选择运行哪个build

当运行gradle命令行的时候,Gradle会在当前目录寻找build文件。可以使用了-b这个命令选项来选择执行指定的build文件。如果使用了-b 选项,那么settings.build就不会再起作用了。比如:

案例 4.5 通过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命令选项来代替-b来选择项目的目录,但是在多模块(多项目)项目构建的时候,应该使用-p而不是-b。

案例 4.6 通过项目路径选择项目

gradle -q -p subdir hello 的输出如下:

> gradle -q -p subdir hello

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

 

4.6 强制执行task

许多的task,尤其是Gradle提供的那些,会支持增量build。这样的task会根据他们的输入或者输出数据是否和上次执行一样,来选择执行或者不执行。大家可以在gradle命令输出时看这个task后面是否有UP-TO_DATE 来判断这个task是否被增量build了。

有时候我们可能想强制Gradle执行所有的task,那么就可以使用--rerun-tasks命令选项,下面是使用这个命令选项和不使用的区别:

案例4.7 强制task运行

gradle doIt输出:

> gradle doIt

:doIt UP-TO-DATE

gradle --rerun-tasks doIt 输出:

> gradle --rerun-tasks doIt

:doIt

注意,这个选项不仅仅会强制执行所声明的task,而是将所有所需的task都强制执行。这个其实类似于清理(clean),但是却不删除之前build所生成的文件.

 

4.7 获取build的信息

Gradle提供了几个内置的task,用来打印build中的一些细节。这个对理解build中的结构和task的依赖关系,以及debugg 问题是有很大用处的。

另外,如果想查看内置task信息,可以将project report plugin加入到项目中去,这个插件能够起到这个作用。

 

4.7.1 罗列项目

运行gradle projects命令会按照继承关系罗列出选中项目的所有子项目,例如:

案例 4.8 获取项目的信息

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

报告就会展示每个声明了描述的项目的描述。大家可以通过description这个属性来为每个项目来提供描述。

 

案例4.9 为项目提供描述

build.gradle文件

description = 'The shared API for the application'

 

4.7.2 罗列task

运行gradle tasks 命令会罗列对应项目的所有主要task。报告中会展示项目的默认task,以及项目中的所有task和他们的描述信息。下面是一个报告的例子:

案例4.10 获取task的信息

gralde -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组注册了的task,也就是所谓的可见(visible)task。我们可以通过设置group这个属性来实现注册。当然亦可以通过description这个属性,来为报告提供task的有关信息。

案例4.11 改变task报告的内容

build.gradle

dists {

    description = 'Builds the distribution'

    group = 'build'

}

大家可以通过--all 这个命令选项来罗列task的更多的信息。这个命令参数会罗列项目中所有task,包括那些没有向task组注册的task,也就是所谓的隐藏(hidden)task。下面是一个例子:

案例4.12 获取task更多的信息

 

gradle -q task --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

 

4.7.3 展示task的使用详情

运行 gradle help --task 某些task ,Gradle就会打印出指定的一个或者多个task的详细信息。下面是一个例子:

案例 4.13 获取task的帮助详情

gradle -q help --task libs的输出:

> gradle -q help --task libs

Detailed task information for libs

 

Paths

     :api:libs

     :webapp:libs

 

Type

     Task (org.gradle.api.Task)

 

Description

     Builds the JAR

 

Group

     build

这个信息包括了task的路径,task的类型,相关的命令行参数和一些描述。

 

4.7.4 罗列项目依赖

运行命令:gradle dependencies 会罗列出指定项目的依赖项目,每个列表都是一个配置。每个配置中的直接和简介依赖都会以树状图展示,下面是一个例子:

 

案例4.14 获取依赖信息

 

gradle -q dependencies api:dependencies webapp:dependencies的输出:

> gradle -q dependencies api:dependencies webapp:dependencies

 

------------------------------------------------------------

Root project

------------------------------------------------------------

 

No configurations

 

------------------------------------------------------------

Project :api - The shared API for the application

------------------------------------------------------------

 

compile

\--- org.codehaus.groovy:groovy-all:2.4.10

 

testCompile

\--- junit:junit:4.12

     \--- org.hamcrest:hamcrest-core:1.3

 

------------------------------------------------------------

Project :webapp - The Web application implementation

------------------------------------------------------------

 

compile

+--- project :api

|    \--- org.codehaus.groovy:groovy-all:2.4.10

\--- commons-io:commons-io:1.2

 

testCompile

No dependencies

因为依赖报告可能会很长,所以能够限制具体的配置是比较有用的,这个可以通过--configuration 参数来实现,

 

案例4.15 通过configuration来过滤依赖

 

gradle -q api:dependencies --configuration testCompile的输出

> gradle -q api:dependencies --configuration testCompile

 

------------------------------------------------------------

Project :api - The shared API for the application

------------------------------------------------------------

 

testCompile

\--- junit:junit:4.12

     \--- org.hamcrest:hamcrest-core:1.3

 

4.7.5 罗列项目build脚本依赖

运行命令:gradle buildEvironment 会展示对应项目的构建脚本依赖,类似于gradle 依赖是如何展示被构建软件的依赖的。

 

4.7.6洞察制定依赖

运行命令:gradle dependencyInsight 会展示制定依赖的内部信息,下面是个例子:

gradle -q webapp:dependencyInsight --dependency groovy --configuration compile的输出:

> gradle -q webapp:dependencyInsight --dependency groovy --configuration compile

org.codehaus.groovy:groovy-all:2.4.10

\--- project :api

     \--- compile

 

这个task对于研究依赖选择,查找依赖从何而来,和为什么要选择某个版本是很有用处的。

想获取更多了解,请查看API文档的 DependencyInsightReportTask 类。

内置dependencyInsight task是 ‘Help’任务组的一部分,这个task需要需要和dependency以及configuration联合使用。报告会查找指定配置中的指定的依赖。如果使用了Java相关的插件,那么dependencyInsight这个task会配置在‘compile’配置之前,因为通常我们很关心编译依赖。大家可以通过--dependency参数来指定想要的依赖。如果不想使用默认配置可以通过--configuration指定想要的配置。

 

4.7.7罗列项目属性

运行命令:gradle properties会罗列处制定项目的属性。下面是一个例子:

案例4.17 属性的信息

gradle -q api:properties的输出

> gradle -q api:properties

 

------------------------------------------------------------

Project :api - The shared API for the application

------------------------------------------------------------

 

allprojects: [project ':api']

ant: org.gradle.api.internal.project.DefaultAntBuilder@12345

antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@12345

artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@12345

asDynamicObject: DynamicObject for project ':api'

baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@12345

buildDir: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build

buildFile: /home/user/gradle/samples/userguide/tutorial/projectReports/api/build.gradle

 

4.7.8记录一个build

--profile命令参数会记录一些build中的时间信息,并将报告保存到build/reports/profile目录中去,这个报告会以当时的时间来命名。这个报告会罗列时间总结和配置阶段以及task执行的一些细节。配置和task执行的时间会按照由大到小排列。task执行结果会显示出那些task被跳过以及跳过的原因,还有哪些task没有被跳过到却没有生效。

那些使用了buildSrc目录的build会在buildSrc/build 生成另一份有关buildSrc的报告。

 Profile

4.8空跑

有时候只是想看下给定的一系列task的执行顺序等情况,并不想让他们真的去执行。这时候可以使用-m参数。比如 gradle -m clean compile 会展示出clean和compile有关的所有task。这个是对tasks这个task的补充。

 

4.9总结

本章,我们了解了Gradle的命令行的一些东西。大家可以在https://docs.gradle.org/4.3.1/userguide/gradle_command_line.html看到更多关于Gradle命令行的东西。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值