版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SCHOLAR_II/article/details/83347808
学习建议
学习路径
- 了解Groovy基本语法, 以及Groovy与Java、Gardle、JVM之间的关系
- 粗读Gradle User Guide 和 [Gradle Plugins User] Guide(https://docs.gradle.org/current/userguide/plugins.html)
- 实践再实践,无他,唯手熟尔
Code snippets
打印所有task的输入与输出
如题,分析Gradle构建问题和学习项目的构建逻辑的必备code snippets
说明:
1.知道输入与输出是分析问题的关键,能顺藤摸瓜,定位问题
2.了解输入与输出即于结果去看代码,总比看代码去理解任务的逻辑,特别是gradle这类解析型弱态型系统的代码。
** 如下代码来源于网络 **
//如下代码copy到build.gradle文件即可
gradle.taskGraph.afterTask { task ->
try {
StringBuffer taskDetails = new StringBuffer()
taskDetails << """"-------------
name:$task.name group:$task.group : $task.description
conv:$task.convention.plugins
inputs:
"""
task.inputs.files.each{ it ->
taskDetails << " ${it.absolutePath}\n"
}
taskDetails << "outputs:\n"
task.outputs.files.each{ it ->
taskDetails << " ${it.absolutePath}\n"
}
taskDetails << "-------------"
println taskDetails
}
catch(Exception e) {
}
}
打印所有task的依赖
如题,分析Gradle构建问题和学习项目的构建逻辑的必备code snippets
** 如下代码来源于网络 **
//如下代码copy到build.gradle文件即可
gradle.getTaskGraph().whenReady {
project.tasks.all {
Task t = it
String taskName = it.name
println("--------taskName-----------:" + taskName + " :" + it.getPath())
it.getTaskDependencies().any {
println("-----------------taskName----dependsOn-----------------:")
it.getDependencies(t).findAll() {
println("----------------------------------:" + it.getPath())
}
}
}
}
监控所有任务
优化脚本的时候可能使用,看看那个操作逻辑比较耗时,针对性优化之
** 如下代码来源于网络 **
//如下代码copy到build.gradle文件即可
class TimingsListener implements TaskExecutionListener, BuildListener {
private Clock clock
private timings = []
@Override
void beforeExecute(Task task) {
clock = new org.gradle.util.Clock()
}
@Override
void afterExecute(Task task, TaskState taskState) {
def ms = clock.timeInMs
timings.add([ms, task.path])
task.project.logger.warn "${task.path} took ${ms}ms"
}
@Override
void buildFinished(BuildResult result) {
println "Task timings:"
for (timing in timings) {
if (timing[0] >= 50) {
printf "%7sms %s\n", timing
}
}
}
@Override
void buildStarted(Gradle gradle) {}
@Override
void projectsEvaluated(Gradle gradle) {}
@Override
void projectsLoaded(Gradle gradle) {}
@Override
void settingsEvaluated(Settings settings) {}
}
gradle.addListener new TimingsListener()
常用命令
生成Wrapper脚本
gradlew可以统一构建的gradle构建环境,避免各个环境的差异导致构建失败或产生兼容性的构建问题
gradle wrapper
查看所有tasks
查看所有tasks一方面可以快速上手新的项目的打包,另一方面可以知道引入的插件有那些任务(避免写多余的自定义任务)
./gradlew tasks --all
如下示例
luogw@luogw-MacBook-Pro firsetGradle$ ./gradlew tasks --all
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Application tasks
-----------------
run - Runs this project as a JVM application
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'firsetGradle'.
components - Displays the components produced by root project 'firsetGradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'firsetGradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'firsetGradle'.
dependentComponents - Displays the dependent components of components in root project 'firsetGradle'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'firsetGradle'. [incubating]
projects - Displays the sub-projects of root project 'firsetGradle'.
properties - Displays the properties of root project 'firsetGradle'.
tasks - Displays the tasks runnable from root project 'firsetGradle'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Other tasks
-----------
compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
copyLicense
processResources - Processes main resources.
processTestResources - Processes test resources.
startScripts - Creates OS specific scripts to run the project as a JVM application.
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
BUILD SUCCESSFUL
Total time: 1.07 secs
查看所有prjects/模块
./gradlew projects
查看所有projects/模块的依赖
./gradlew dependencies