Gradle基礎:3:生命週期管理
阿新 • • 發佈:2018-11-20
Maven中的生命週期的管理使用了COC,以此為中心的pom.xml檔案成為了重中之重,優點是不同專案之間的經驗共享變得更加容易,大家大部分都是可以使用類似的套路,缺點則是靈活性稍微降低以及對於pom.xml細節的學習需要較多時間。Gradle則將這些再次放開,給更多的許可權與開發者,這篇文章來簡單看一下Gradle生命週期管理的一些基礎知識。
gradle構建的階段
gradle的構建分為初期化/配置/執行三個階段:
構建的生命週期
在Maven中大部分專案的生命週期基本使用pom規範中定義的那些即可,而在Gradle中,可以通過Task的定義來定製專案自己所需要的方式。這裡將會把前面的HelloWorld的例子進行簡單的修改,模擬常見的maven專案經常會用到的幾個階段:
程式碼示例
- settings.gradle
liumiaocn:hello liumiao$ cat settings.gradle
println "[Phase: initialization] : settings executed... "
rootProject.name='helloPorject'
liumiaocn:hello liumiao$
- build.gradle
在gradle中上述的四個階段,可以使用如下4個task來進行模擬:
liumiaocn:hello liumiao$ cat build.gradle println "[phase:configuration] build.gradle ..." task compile() { group 'customized' description 'compile task' println "[phase:configuration] compile" doFirst { println "[phase:execution] compile :doFirst()" } } task test { group 'customized' description 'test task' println "[phase:configuration] test" doLast { println "[phase:execution] test:doLast()" } } task packaging { group 'customized' description 'packaging task' println "[phase:configuration] packaging" doLast { println "[phase:execution] packaging:doLast()" } } task install { group 'customized' description 'install task' println "[phase:configuration] install" doFirst { println "[phase:execution] install:doFirst()" } doLast { println "[phase:execution] install:doLast()" } } liumiaocn:hello liumiao$
執行確認
確認task
liumiaocn:hello liumiao$ gradle tasks [Phase: initialization] : settings executed... > Configure project : [phase:configuration] build.gradle ... [phase:configuration] compile [phase:configuration] test [phase:configuration] packaging [phase:configuration] install > Task :tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Build Setup tasks ----------------- init - Initializes a new Gradle build. wrapper - Generates Gradle wrapper files. Customized tasks ---------------- compile - compile task install - install task packaging - packaging task test - test task Help tasks ---------- buildEnvironment - Displays all buildscript dependencies declared in root project 'helloPorject'. components - Displays the components produced by root project 'helloPorject'. [incubating] dependencies - Displays all dependencies declared in root project 'helloPorject'. dependencyInsight - Displays the insight into a specific dependency in root project 'helloPorject'. dependentComponents - Displays the dependent components of components in root project 'helloPorject'. [incubating] help - Displays a help message. model - Displays the configuration model of root project 'helloPorject'. [incubating] projects - Displays the sub-projects of root project 'helloPorject'. properties - Displays the properties of root project 'helloPorject'. tasks - Displays the tasks runnable from root project 'helloPorject'. To see all tasks and more detail, run gradle tasks --all To see more detail about a task, run gradle help --task <task> BUILD SUCCESSFUL in 0s 1 actionable task: 1 executed liumiaocn:hello liumiao$
確認執行
需要注意的上文所講到的生命週期,通過對任務的執行可以看地清清楚楚,無論對哪個任務進行執行,初始化和配置部分的程式碼都會被執行,這點是在使用gradle時需要特別注意的。gradle的groovy或者kotlin的程式碼在什麼階段會被執行,一定要理解和注意。
liumiaocn:hello liumiao$ gradle compile
[Phase: initialization] : settings executed...
> Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] packaging
[phase:configuration] install
> Task :compile
[phase:execution] compile :doFirst()
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$
doFrist與doLast
doFirst和doLast都是gradle預留的回撥,在執行階段會被呼叫,兩者的順序是doFirst() -> doLast()
liumiaocn:hello liumiao$ gradle install
[Phase: initialization] : settings executed...
> Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] packaging
[phase:configuration] install
> Task :install
[phase:execution] install:doFirst()
[phase:execution] install:doLast()
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$
多個任務同時執行
多個任務同時執行,就有點類似maven的動作了
liumiaocn:hello liumiao$ gradle compile test packaging install
[Phase: initialization] : settings executed...
> Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] packaging
[phase:configuration] install
> Task :compile
[phase:execution] compile :doFirst()
> Task :test
[phase:execution] test:doLast()
> Task :packaging
[phase:execution] packaging:doLast()
> Task :install
[phase:execution] install:doFirst()
[phase:execution] install:doLast()
BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
liumiaocn:hello liumiao$
總結
本系列中的例子的使用方式均是為了解釋Gradle的基礎概念和基本使用方式所整理,實際專案中如何進行最佳實踐的落地,需要自行探索和研究。這篇文章強調的是gradle使用的時候的生命週期的三個階段,一定要多留意這些與maven之類不同的地方。
參考內容
https://docs.gradle.org/current/userguide/build_lifecycle.html