Gradle學習與使用技巧點的收集與彙總
阿新 • • 發佈:2018-11-27
學習建議
學習路徑
- 瞭解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
如下示例
[email protected] 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