转载请注明出处:http://blog.csdn.net/lastsweetop/article/details/78855894
多任务执行
gradle命令可以同时执行多个任务,参数为任务列表,参数列表中的任务会按顺序执行,例如gradle compile test
,compile任务和test任务都会被执行,包括test依赖的任务,但要注意的一点是,同一个任务只会执行一次,不管是列表中的任务,还是列表任务依赖的任务,同一个任务只会执行一次。
我们来看一个例子,这里有四个任务,任务图如下:
这里有四个任务,dist和test都依赖于compile,但是执行gradle compile test
命令,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'
}
}
输出:
> Task :compile
compiling source
> Task :compileTest
compiling unit tests
> Task :test
running unit tests
BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed
每个任务都执行了一次,很容易可以推测gradle test test
也是只会执行一次。
排除任务
gradle dist -x test
命令的输出如下
> Task :compile
compiling source
> Task :dist
building the distribution
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
你可以看到,尽管dist任务依赖于test任务,但是使用-x之后test任务并没有执行,test依赖的compileTest任务也没执行,test也依赖于compile任务,但是compile同时也会被dist依赖,并且没有排除,因此compile执行了。
忽略失败继续执行
默认情况下,一旦一个任务失败,Gradle就会终止运行并且构建失败,这样可以快速完成构建,但是会因此了其他可能出现的构建问题。为了可以在一次构建中暴露更多的问题,这时候就需要-continue
参数,有了这个参数,即使一个任务失败了,其他任务仍然会继续执行,然后所有的错误在构建完成后统一列出。
但要注意的一点是,这里的其他任务不包括依赖于已失败任务的其他任务,一旦一个任务失败,及时有-continue
参数,依赖于它的其他任务也不会执行。比如compileTest任务已经失败了,那么依赖于它的test任务和dist任务,都不会执行。
任务名缩写
当你在命令行中指定任务名称的时候,你不需要输入完整的任务名,你只需要输入可以唯一标准次任务的缩写名就可以了,比如dist你可以缩写成di, gradle di
的输入如下:
> Task :compile
compiling source
> Task :compileTest
compiling unit tests
> Task :test
running unit tests
> Task :dist
building the distribution
BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed
很多时候前面几个字母有很多相同的,那要输入很多才会唯一标识的任务名,不怕,缩写还支持首字母的骆驼命名法,比如compileTest可以直接缩写成cT gradle cT
的输出如下:
> Task :compile
compiling source
> Task :compileTest
compiling unit tests
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
-x 的参数也可以这样玩
指定build配置文件
执行gradle命令时,他会默认寻找当前目录下的build.gradle文件。当然你可以通过-b
指定要执行的build文件(-b
=--build-file
).
在当前目录下,创建subdir/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
标签是用来屏蔽其他辅助型输出的,只输出任务的输出流或者错误流(-q
=--quiet
)
还有一种选择就是使用-p
指令(-p
=--project-dir
),常用于多个项目的构建,当然文件名要使用默认的buil.gradle了 gradle -q -p subdir hello
指令的输出如下:
using build file 'build.gradle' in 'subdir'
强制任务执行
Gradle提供的大部分任务都是支持增量更新的,Gradle可以在任务运行的时候根据任务的输入输出的改变来判断任务是否需要运行。在构建的运行时,build会给任务打上UP-TO-DATE
的标签,一眼就可以知道哪些任务是增量更新的。
有时候你需要强制执行任务,而忽略up-to-date检查,这也很简单,只需要加上--rerun-tasks
没有--rerun-tasks
的输出
> gradle jar
BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 up-to-date
加上--rerun-tasks
的输出
> gradle --rerun-tasks jar
BUILD SUCCESSFUL in 7s
4 actionable tasks: 4 executed
注意加上--rerun-tasks
后不仅仅是当前任务要执行,还包括需要执行该任务之前的任务都要重新执行
查看构建信息
Gradle提供了几个内置的任务,通过这些任务可以查看构建的详细信息。可以帮助理解构建的依赖结构,debug错误。
项目列表
gradle projects
可以查看所选项目及子项目的列表信息,结果以层级结构输出。 gradle -q projects
的输出如下:
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'studygradle'
\--- Project ':subdir' - this is a subproject
To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :subdir:tasks
列表中还有功能的描述信息,给项目加上描述信息是个好习惯,在build.gradle文件中增加即可:
description = 'this is a subproject'
任务列表
gradle tasks
可以查看所选项目的主要任务列表,显示方式和项目列表一样。 gradle -q tasks
输出如下:
------------------------------------------------------------
All tasks runnable from root project - this is a root project
------------------------------------------------------------
Build tasks
-----------
compile
dist - this is dist task
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 'studygradle'.
components - Displays the components produced by root project 'studygradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'studygradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'studygradle'.
dependentComponents - Displays the dependent components of components in root project 'studygradle'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'studygradle'. [incubating]
projects - Displays the sub-projects of root project 'studygradle'.
properties - Displays the properties of root project 'studygradle'.
tasks - Displays the tasks runnable from root project 'studygradle' (some of the displayed tasks may belong to subprojects).
Test tasks
----------
compileTest
test
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
默认情况下,报告只显示那些已经被明确分组的任务,这些任务可以统称为可见任务。要想变为可见任务,需要任务设置group
属性,另外也可以设置任务的description
属性,在定义任务的时候或者定义任务结束后都可以设置,方法如下:
compile {
description 'compile task'
group 'build'
}
想获得更多任务信息的话,需要增加--all
的选型,这将列出所选项目的所有任务,包括没有分组的隐藏任务。 gradle -q tasks --all
的输出如下:
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
compile - compile task
dist
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 'studygradle'.
subdir:buildEnvironment - Displays all buildscript dependencies declared in project ':subdir'.
components - Displays the components produced by root project 'studygradle'. [incubating]
subdir:components - Displays the components produced by project ':subdir'. [incubating]
dependencies - Displays all dependencies declared in root project 'studygradle'.
subdir:dependencies - Displays all dependencies declared in project ':subdir'.
dependencyInsight - Displays the insight into a specific dependency in root project 'studygradle'.
subdir:dependencyInsight - Displays the insight into a specific dependency in project ':subdir'.
dependentComponents - Displays the dependent components of components in root project 'studygradle'. [incubating]
subdir:dependentComponents - Displays the dependent components of components in project ':subdir'. [incubating]
help - Displays a help message.
subdir:help - Displays a help message.
model - Displays the configuration model of root project 'studygradle'. [incubating]
subdir:model - Displays the configuration model of project ':subdir'. [incubating]
projects - Displays the sub-projects of root project 'studygradle'.
subdir:projects - Displays the sub-projects of project ':subdir'.
properties - Displays the properties of root project 'studygradle'.
subdir:properties - Displays the properties of project ':subdir'.
tasks - Displays the tasks runnable from root project 'studygradle' (some of the displayed tasks may belong to subprojects).
subdir:tasks - Displays the tasks runnable from project ':subdir'.
Test tasks
----------
compileTest
test
Other tasks
-----------
subdir:hello
任务的用例细节
gradle help --task someTask
可以列出所选任务的详细信息,如果有多个任务匹配给出的名字,那么将多个任务同时列出。 gradle help --task compile
命令的输出如下:
Detailed task information for compile
Paths
:compile
:subdir:compile
Type
Task (org.gradle.api.Task)
Descriptions
(:compile) compile root
(:subdir:compile) compile subdir
Group
build
信息包括给定任务的任务路径,任务类型,任务描述和任务分组信息
列出工程依赖
gradle dependencies
会根据配置列出选定项目的依赖,针对每个配置,直接依赖和传递依赖都会被列出, gradle -q dependencies subdir:dependencies
命令的输出如下:
------------------------------------------------------------
Root project
------------------------------------------------------------
No configurations
------------------------------------------------------------
Project :subdir - this is a subproject
------------------------------------------------------------
apiElements - API elements for main. (n)
No dependencies
archives - Configuration for archive artifacts.
No dependencies
compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
\--- commons-cli:commons-cli:1.4
compileClasspath - Compile classpath for source set 'main'.
\--- commons-cli:commons-cli:1.4
compileOnly - Compile only dependencies for source set 'main'.
No dependencies
default - Configuration for default artifacts.
\--- commons-cli:commons-cli:1.4
implementation - Implementation only dependencies for source set 'main'. (n)
No dependencies
runtime - Runtime dependencies for source set 'main' (deprecated, use 'runtimeOnly ' instead).
\--- commons-cli:commons-cli:1.4
runtimeClasspath - Runtime classpath of source set 'main'.
\--- commons-cli:commons-cli:1.4
runtimeElements - Elements of runtime for main. (n)
No dependencies
runtimeOnly - Runtime only dependencies for source set 'main'. (n)
No dependencies
testCompile - Dependencies for source set 'test' (deprecated, use 'testImplementation ' instead).
+--- commons-cli:commons-cli:1.4
\--- junit:junit:4.0
testCompileClasspath - Compile classpath for source set 'test'.
+--- commons-cli:commons-cli:1.4
\--- junit:junit:4.0
testCompileOnly - Compile only dependencies for source set 'test'.
No dependencies
testImplementation - Implementation only dependencies for source set 'test'. (n)
No dependencies
testRuntime - Runtime dependencies for source set 'test' (deprecated, use 'testRuntimeOnly ' instead).
+--- commons-cli:commons-cli:1.4
\--- junit:junit:4.0
testRuntimeClasspath - Runtime classpath of source set 'test'.
+--- commons-cli:commons-cli:1.4
\--- junit:junit:4.0
testRuntimeOnly - Runtime only dependencies for source set 'test'. (n)
No dependencies
信息太多,有时候我们只关注一部分信息,这时就需要增加--configuration
选项, gradle -q subdir:dependencies --configuration testCompile
命令输出如下:
------------------------------------------------------------
Project :subdir - this is a subproject
------------------------------------------------------------
testCompile - Dependencies for source set 'test' (deprecated, use 'testImplementation ' instead).
+--- commons-cli:commons-cli:1.4
\--- junit:junit:4.0
项目构建脚本的依赖列表
gradle buildEnvironment
命令可以列出选定项目构建脚本需要的依赖,和gradle dependencies
的使用方法非常类似。 gradle -q subdir:buildEnvironment
命令的输出如下:
------------------------------------------------------------
Project :subdir - this is a subproject
------------------------------------------------------------
classpath
\--- net.saliman:gradle-cobertura-plugin:2.5.2
单个依赖深度分析
gradle dependencyInsight
可以对单个依赖进行深度分析。 gradle -q subdir:dependencyInsight --dependency junit --configuration testCompile
命令的输出如下:
junit:junit:4.0
\--- testCompile
这个命令在分析依赖关系时非常有用,可以找出当前依赖来自于哪里,为什么是这个版本等信息。这个内置的任务属于help
任务组,可以配置dependency
和configuration
选项。报告会查找与configuration
参数相同配置并且匹配dependency
参数的依赖。如果是java插件,那么默认的configuration
是compileClasspath,这是我们最关心的配置。
项目属性
gradle properties
可以列出选定项目的所有的属性。 gradle -q subdir:properties
命令输出如下
allprojects: [project ':subdir']
ant: org.gradle.api.internal.project.DefaultAntBuilder@1f28c5b3
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@16700e92
archivesBaseName: subdir
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@7f2f297
asDynamicObject: DynamicObject for project ':subdir'
assemble: task ':subdir:assemble'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@5d703d8a
buildDependents: task ':subdir:buildDependents'
buildDir: /Users/apple/Documents/mydream/gradle/studygradle/subdir/build
buildFile: /Users/apple/Documents/mydream/gradle/studygradle/subdir/build.gradle
....
构建剖析
在每个命令后面都可以增加--profile
选项,他会在root项目build/reports/profile
目录下生成html的报告,报告以命令的执行时间命名。这个报告列出了配置阶段和任务执行阶段的招摇时间和详细信息,配置时间和任务执行时间倒序排列,跳过不执行的任务一样会列出来。
如果执行过程中用到了buildSrc,buildSrc/build
目录下会生产第二份报告,如图所示:
空转
有时候你仅仅对多个任务的执行顺序感兴趣,但是并不希望任务被执行,那么可以增加-m
选项(-m
=--dry-run
) gradle -qm test dist
命令的输出如下:
:compile SKIPPED
:compileTest SKIPPED
:test SKIPPED
:subdir:compileJava SKIPPED
:subdir:processResources SKIPPED
:subdir:classes SKIPPED
:subdir:compileTestJava SKIPPED
:subdir:processTestResources SKIPPED
:subdir:testClasses SKIPPED
:subdir:test SKIPPED
:dist SKIPPED
所以任务都直接跳过