官方 5.0 Release Note 鏈接:docs.gradle.org/5.0/release…
前言
在歷經(jīng)了一年多時間, 20 個 4.x 系列版本的迭代后,Gradle 官方終于在 11月26日 發(fā)布了 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
我擦,感覺一臉懵逼,沒關(guān)系,下面我會針對這些特性做些簡要的總結(jié)和說明,內(nèi)容可能比較多,但是干貨滿滿,建議耐心閱讀下去。
Kotlin DSL 1.0
早在 Gradle 4.x 版本中就已經(jīng)支持了通過使用 Koltin DSL 的方式去編寫構(gòu)建腳本,但是當(dāng)時剛出來,很多地方支持的還不太好(需要踩坑),所以并不推薦大家遷移過去,而是嘗鮮為主?,F(xiàn)在 Kotlin DSL 1.0 release 版本出來后,意味著接下來你可以放心的遷移或者使用 Kotlin DSL 啦。新版本做了很多優(yōu)化和改進的地方,比如:
Code Completion:代碼自動完成
Error Highlighting:錯誤高亮
Quick Documentation:文檔快速提示
Refactoring:代碼重構(gòu)
如何遷移你的構(gòu)建語言到 Kotlin DSL 上,可以參考我的另一篇文章Gradle指南之從Groovy遷移到Kotlin
Dependency version alignment
Dependency version alignment allows different modules belonging to the same logical group (a platform) to have identical versions in a dependency graph.
根據(jù)官網(wǎng)的介紹,直譯過來的意思是:依賴版本一致性允許屬于相同的邏輯組(平臺)的不同module 擁有相同的版本依賴圖。
這個概念確實不好理解,我自己也是花了一些時間去理解和消化。還是通過一個示例來說明吧。如果有英文水平高或者理解能力比較強的同學(xué),歡迎指教。
比如,我們 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'
}復(fù)制代碼因為 vertx-core 也間接依賴了 jackson-core,通過依賴傳遞,當(dāng)解析依賴信息的時候,我們實際依賴的版本號是:
jackson-core版本 2.9.5(依賴傳遞)jackson-databind版本 2.8.9 -> 2.9.5(低版本通過依賴仲裁變?yōu)楦甙姹荆?/p>jackson-annotation版本 2.9.0 (jackson-databind2.8.9 版本間接依賴jackson-annotation2.9.0 版本,注意這里,因為上面的依賴仲裁,變成了被高版本 的jackson-databind2.9.5 所依賴)
通過以上的分析,我們發(fā)現(xiàn)實際上解析出來的依賴庫版本出現(xiàn)了和預(yù)期不一致的情況,而類似這種情況很容易會導(dǎo)致一些未知問題,尤其是在一些不兼容老版本接口的依賴庫里就會直接導(dǎo)致 Crash。
通常,針對這種問題,之前的做法可能就是簡單粗暴的強指定版本號,不過,現(xiàn)在有了更加優(yōu)雅的方式去幫助我們?nèi)ス芾戆姹?,?Dependency version alignment,我們可以通過指定一個 platform 去管理某一組的 module。
回到剛才的場景示例中,我們在 build.gradle 里可以定義 jackson 為一組的 module 版本的規(guī)則,如下所示:
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}")
}
}
}
}復(fù)制代碼然后應(yīng)用該規(guī)則:
dependencies {
components.all(JacksonAlignmentRule)
}復(fù)制代碼最后,在執(zhí)行完 ./gradlew app:dependencies 命令后,你會發(fā)現(xiàn)當(dāng)匹配上 com.fasterxml.jackson 組的 module 的版本都保持了一致,也就是
jackson-core版本 2.9.5jackson-databind版本 2.8.9 -> 2.9.5jackson-annotation版本 2.9.0 -> 2.9.5
另外,platform 同樣也提供了強制為一組 module 指定某個版本號,比如:
dependencies {
// Forcefully downgrade the Jackson platform to 2.8.9
implementation enforcedPlatform('com.fasterxml.jackson:jackson-platform:2.8.9')
}復(fù)制代碼還有一個比較方便的地方,就是通過 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")
}復(fù)制代碼Gradle build initialization features
新版本對 gradle init 方法進行了升級,通過更多的特性和交互,來幫助我們?nèi)タ焖俪跏蓟粋€ Gradle 項目。簡單來說的話,就是加強版的腳手架。
Interactive mode
當(dāng)你在控制臺執(zhí)行 init task 后,Gradle 將會提供更多的構(gòu)建信息提示,來幫助你生成 Gradle 模版項目。
Kotlin library and applications
init task 可以通過 kotlin-library 或者 kotlin-application 類型來設(shè)置生成一個 Kotlin 的類庫或者應(yīng)用。你只需要根據(jù)它的提示來操作即可。
Generated builds use recommended configurations
init task 生成的構(gòu)建腳本將推薦使用新的 implementation、testImplementation、testRuntimeOnly 去代替 compile、testCompile、testRuntime。
Configure project and source package names
--project-name選項可以幫助你調(diào)整生成的項目名稱--package選項可以幫助你調(diào)整生成的項目包名
Create resource directories
init task 會創(chuàng)建一個空的 resource 目錄
Create a .gitignore file
init task 會生成一個簡單的 .gitignore 文件來幫助你設(shè)置你的 Git repository 。
Searchable documentation
Gradle 的文檔搜索功能回來了(雖然我一直不知道這個功能在哪里),總之,可以更加方便的搜索用戶手冊和 DSL 的描述。
當(dāng)然,還有更方便查找類和方法的 Gradle API Javadocs。
Task timeouts
這個超時設(shè)置簡直太有用了有木有。特別是當(dāng)你因為一些網(wǎng)絡(luò)原因,導(dǎo)致你的 build task 一直卡在那里的時候,你的心里一定是萬馬奔騰的感覺,趕緊用起來吧,不要再浪費這些無用的時間了。示例:
task hangingTask() {
doLast {
Thread.sleep(100000)
}
timeout = Duration.ofMillis(500)
}復(fù)制代碼HTTP retries during dependency resolution
HTTP 訪問重試功能,嗯。挺好
Performance features
Gradle can be started as a low-priority process
通過 --priority low 命令參數(shù)或者 org.gradle.priority=low 屬性設(shè)置可以讓你的 Gradle 以一個低優(yōu)先級的進程啟動,至于好處嘛,當(dāng)然是你一邊在 building 的時候,一邊聽音樂的時候,再也不會感覺一卡一卡的了 :)
JaCoCo plugin now works with the build cache and parallel test execution
簡單看了下介紹,可以幫助你查看代碼覆蓋率的 JaCoCo 插件,感興趣的同學(xué)可以點這里了解更多
Java 11 runtime support
Gradle 5.0 版本構(gòu)建支持 Java 11 版本
Plugin authoring features
這個版本為插件和自定義 task 提供了創(chuàng)建 SourceDirectorySet 的 API、提高 Provider 的 API、和構(gòu)建緩存的兼容性提高。
Gradle Native features
Gradle Native project 持續(xù)的改善和提升 native 生態(tài)系統(tǒng), 更多細(xì)節(jié):Changes included in Gradle 5.0
Promoted features
一些現(xiàn)有功能的提升,詳細(xì)點擊這里
其他
5.0 版本共計修復(fù)了 166 個 issues(驚人),當(dāng)然,伴隨而來的還有一些已知的 issues ,詳細(xì)可以點擊這里 5.0 release-notes。
總結(jié)
Gradle 5.0 版本真的是干貨滿滿,尤其是對于使用 Gradle 構(gòu)建的 Android 或者 Java 開發(fā)者來說,像 Kotlin DSL(構(gòu)建語言)、Dependency version alignment(依賴版本一致性)、Performance features(性能提升) 等等這些特性還是很讓人期待的。有興趣的小伙伴可以趕緊更新一波了。
如何升級到 Gradle 5.0 版本呢,很簡單,以下兩種方式任選其一:
執(zhí)行
./gradlew wrapper --gradle-version=5.0命令或者直接修改你的
rootProject/gradle/wrapper/gradle-wrapper.propertiesdistributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists





暫無評論,快來評論吧!