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





暫無(wú)評(píng)論,快來(lái)評(píng)論吧!