Gradle 4.x 升級 Gradle 5.x
最近開始複習以前自己寫過的老項目,不過就是2018年寫的項目,今天打開發現套件根本無法從網路上獲取,各種錯誤,填了好久的坑終於搞定,馬上筆記下來。
前前後後大概遇到的錯誤訊息如下
- Failed to apply plugin [id 'com.gradle.build-scan']
- Could not resolve all artifacts for configuration ':classpath'
- Could not resolve all dependencies for configuration ':detachedConfiguration1'
這就很莫名其妙,始終沒有說明是需要升級 Gradle 但是套件就是無法從網路上獲取,最初以為是套件庫的問題,自己跑去架設 Nexus3 發現還是無解,還是無法正常獲取套件,然而我重新寫新項目又都正常,於是乎開始懷疑是 Gradle 版本問題。
直到我刪除 org.springframework.boot 相關依賴後重新 Gradle Build 出現下圖,確認是版本問題!
參考這篇文章 Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 才發現人家大部分用 Gradle 都是 Android 開發者搭配 Kotlin 才會遇到的坑,怎麼我寫 Spring boot 也會遇到各種套件依賴的坑,但新增項目又正常於是參考這篇文章開始升級項目的 Gradle 版本。
修改 gradle-wrapper.properties
找到項目底下 \gradle\wrapper 有個 gradle-wrapper.properties 記事本打開 修改如下,把 distributionUrl 改成最新的版本即可
1 2 3 4 5 6 |
#Sun Feb 24 10:09:15 CST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip |
然後回到 IntelliJ 項目中,刪除之前 .gradle 和 build 重新執行 Gradle Build 這時等待下載新版本,如果 build.gradle 配置沒錯應該就正常,但實際上會出現很多錯誤如下繼續填坑。
解決 build.gradle 錯誤
解決 Failed to apply plugin [id 'com.gradle.build-scan']
遇到這個錯誤的原因是 Gradle 版本太高,但 gradle build tool 版本過低導致不能編譯,詳細參考官方網站
解決 Failed to apply plugin [id 'net.ltgt.apt']
* What went wrong:
A problem occurred evaluating root project 'dinglog'.
> Failed to apply plugin [id 'net.ltgt.apt']
> org.gradle.api.internal.tasks.DefaultTaskInputFilePropertyRegistration cannot be cast to org.gradle.api.tasks.TaskInputs
net.ltgt.gradle:gradle-apt-plugin:0.13 直接升級 0.16 版本如下
1 2 3 4 5 6 7 8 9 |
buildscript { xxxxxx...... dependencies { classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.0.RELEASE' classpath "com.gradle:build-scan-plugin:2.3" classpath 'io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE' classpath "net.ltgt.gradle:gradle-apt-plugin:0.16" } } |
這時應該可以正常執行項目,引用也都正常,如下提示
> Configure project :
The apt configuration has been deprecated. Please use the annotationProcessor configuration instead.CONFIGURE SUCCESSFUL in 15s
因為項目中使用 lombok 其中引用的方式如下
1 2 3 4 5 |
dependencies { xxxxx... compileOnly 'org.projectlombok:lombok:1.18.4' apt 'org.projectlombok:lombok:1.18.4' } |
建議改為下述,即可完成升級
1 2 3 4 5 6 |
dependencies { compileOnly 'org.projectlombok:lombok:1.18.4' annotationProcessor 'org.projectlombok:lombok:1.18.4' testCompileOnly 'org.projectlombok:lombok:1.18.4' testAnnotationProcessor 'org.projectlombok:lombok:1.18.4' } |
具體參考 Gradle的依赖方式——Lombok在Gradle中的正确配置姿势 以及關於 apt 這個依賴做什麼用的可以參考該 issue
相逢就是有緣,留下足跡吧!