官方 5.0 Release Note 連結: https://docs.gradle.org/5.0/release-notes.html
前言
在曆經了一年多時間, 20 個 4.x 系列版本的疊代後,Gradle 官方終于在 11月26日 釋出了 5.0 的正式版本,讓我們趕緊來看一下有哪些令人激動的新特性。
官方新特性一覽
- Kotlin DSL 1.0
- Dependency version alignment
- Gradle build initialization features
- Searchable documentation
- Task timeouts
- HTTP retries during dependency resolution
- Java 11 runtime support
- Plugin authoring features
- Gradle Native features
- Promoted features
早在 Gradle 4.x 版本中就已經支援了通過使用 Koltin DSL 的方式去編寫建構腳本,但是當時剛出來,很多地方支援的還不太好(需要踩坑),是以并不推薦大家遷移過去,而是嘗鮮為主。現在 Kotlin DSL 1.0 release 版本出來後,意味着接下來你可以放心的遷移或者使用 Kotlin DSL 啦。新版本做了很多優化和改進的地方,比如:
- Code Completion:代碼自動完成
- Error Highlighting:錯誤高亮
- Quick Documentation:文檔快速提示
- Refactoring:代碼重構
![圖檔來自官方](https://user-gold-cdn.xitu.io/2018/11/27/16753ed585f2bd3f?w=1340&h=746&f=png&s=649481)
如何遷移你的建構語言到 Kotlin DSL 上,可以參考我的另一篇文章[Gradle指南之從Groovy遷移到Kotlin
](
https://juejin.im/post/5b875c9c6fb9a01a0d74c34b)Dependency version alignment allows different modules belonging to the same logical group (a platform) to have identical versions in a dependency graph.
根據官網的介紹,直譯過來的意思是:依賴版本一緻性允許屬于相同的邏輯組(平台)的不同module 擁有相同的版本依賴圖。
這個概念确實不好了解,我自己也是花了一些時間去了解和消化。還是通過一個示例來說明吧。如果有英文水準高或者了解能力比較強的同學,歡迎指教。
比如,我們
build.gradle
有以下依賴:
dependencies {
// a dependency on Jackson Databind
implementation 'com.fasterxml.jackson.core:jackson-databind:2.8.9'
// and a dependency on vert.x
implementation 'io.vertx:vertx-core:3.5.3'
}
因為
vertx-core
也間接依賴了
jackson-core
,通過依賴傳遞,當解析依賴資訊的時候,我們實際依賴的版本号是:
-
版本 2.9.5(依賴傳遞)jackson-core
-
版本 2.8.9 -> 2.9.5(低版本通過依賴仲裁變為高版本)jackson-databind
-
版本 2.9.0 (jackson-annotation
2.8.9 版本間接依賴jackson-databind
2.9.0 版本,注意這裡,因為上面的依賴仲裁,變成了被高版本 的jackson-annotation
2.9.5 所依賴)jackson-databind
通過以上的分析,我們發現實際上解析出來的依賴庫版本出現了和預期不一緻的情況,而類似這種情況很容易會導緻一些未知問題,尤其是在一些不相容老版本接口的依賴庫裡就會直接導緻 Crash。
通常,針對這種問題,之前的做法可能就是簡單粗暴的強指定版本号,不過,現在有了更加優雅的方式去幫助我們去管理版本,即
Dependency version alignment
,我們可以通過指定一個
platform
去管理某一組的
module
。
回到剛才的場景示例中,我們在
build.gradle
裡可以定義
jackson
為一組的
module
版本的規則,如下所示:
class JacksonAlignmentRule implements ComponentMetadataRule {
void execute(ComponentMetadataContext ctx) {
ctx.details.with {
if (id.group.startsWith("com.fasterxml.jackson")) {
// declare that Jackson modules all belong to the Jackson virtual platform
belongsTo("com.fasterxml.jackson:jackson-platform:${id.version}")
}
}
}
}
然後應用該規則:
dependencies {
components.all(JacksonAlignmentRule)
}
最後,在執行完
./gradlew app:dependencies
指令後,你會發現當比對上
com.fasterxml.jackson
組的
module
的版本都保持了一緻,也就是
-
版本 2.9.5jackson-core
-
版本 2.8.9 -> 2.9.5jackson-databind
-
版本 2.9.0 -> 2.9.5jackson-annotation
另外,
platform
同樣也提供了強制為一組
module
指定某個版本号,比如:
dependencies {
// Forcefully downgrade the Jackson platform to 2.8.9
implementation enforcedPlatform('com.fasterxml.jackson:jackson-platform:2.8.9')
}
還有一個比較友善的地方,就是通過
platform
,我們可以直接省略該
module
的版本号(從此告别通過定義版本變量去維護衆多
module
的版本資訊),如下所示:
dependencies {
// import a BOM. The versions used in this file will override any other version found in the graph
implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE"))
// define dependencies without versions
implementation("com.google.code.gson:gson")
implementation("dom4j:dom4j")
// this version will be overriden by the one found in the BOM
implementation("org.codehaus.groovy:groovy:1.8.6")
}
新版本對
gradle init
方法進行了更新,通過更多的特性和互動,來幫助我們去快速初始化一個 Gradle 項目。簡單來說的話,就是加強版的腳手架。
Interactive mode
當你在控制台執行
init
task 後,Gradle 将會提供更多的建構資訊提示,來幫助你生成 Gradle 模版項目。
Kotlin library and applications
init
task 可以通過
kotlin-library
或者
kotlin-application
類型來設定生成一個 Kotlin 的類庫或者應用。你隻需要根據它的提示來操作即可。
Generated builds use recommended configurations
init
task 生成的建構腳本将推薦使用新的
implementation
、
testImplementation
testRuntimeOnly
去代替
compile
testCompile
testRuntime
Configure project and source package names
-
選項可以幫助你調整生成的項目名稱--project-name
-
選項可以幫助你調整生成的項目包名--package
Create resource directories
init
task 會建立一個空的 resource 目錄
Create a .gitignore file
init
task 會生成一個簡單的
.gitignore
檔案來幫助你設定你的 Git repository 。
Gradle 的文檔搜尋功能回來了(雖然我一直不知道這個功能在哪裡),總之,可以更加友善的搜尋使用者手冊和 DSL 的描述。
當然,還有更友善查找類和方法的
Gradle API Javadocs這個逾時設定簡直太有用了有木有。特别是當你因為一些網絡原因,導緻你的
build task
一直卡在那裡的時候,你的心裡一定是萬馬奔騰的感覺,趕緊用起來吧,不要再浪費這些無用的時間了。示例:
task hangingTask() {
doLast {
Thread.sleep(100000)
}
timeout = Duration.ofMillis(500)
}
HTTP 通路重試功能,嗯。挺好
Performance features
Gradle can be started as a low-priority process
通過
--priority low
指令參數或者
org.gradle.priority=low
屬性設定可以讓你的 Gradle 以一個低優先級的程序啟動,至于好處嘛,當然是你一邊在
building
的時候,一邊聽音樂的時候,再也不會感覺一卡一卡的了 :)
JaCoCo plugin now works with the build cache and parallel test execution
簡單看了下介紹,可以幫助你檢視代碼覆寫率的 JaCoCo 插件,感興趣的同學可以
點這裡了解更多Gradle 5.0 版本建構支援 Java 11 版本
這個版本為插件和自定義 task 提供了建立
SourceDirectorySet
的 API、提高
Provider
的 API、和建構緩存的相容性提高。
Gradle Native project 持續的改善和提升 native 生态系統,
更多細節:
Changes included in Gradle 5.0一些現有功能的提升,
詳細點選這裡其他
5.0 版本共計修複了 166 個 issues(驚人),當然,伴随而來的還有一些已知的 issues ,詳細可以點選這裡
5.0 release-notes總結
Gradle 5.0 版本真的是幹貨滿滿,尤其是對于使用 Gradle 建構的 Android 或者 Java 開發者來說,像 Kotlin DSL(建構語言)、Dependency version alignment(依賴版本一緻性)、Performance features(性能提升) 等等這些特性還是很讓人期待的。有興趣的小夥伴可以趕緊更新一波了。
如何更新到 Gradle 5.0 版本呢,很簡單,以下兩種方式任選其一:
- 執行
指令./gradlew wrapper --gradle-version=5.0
- 或者直接修改你的
rootProject/gradle/wrapper/gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists