Gradle从7.6升级至8.0后报错NoSuchMethodError求助
Gradle 8.0升级构建失败解决方案
问题背景
在macOS Ventura 13.2.1系统上,通过Homebrew将Gradle从7.6版本升级至8.0版本,使用OpenJDK 19.0.2。执行gradle build clean命令时构建失败,报错信息如下:
FAILURE: Build failed with an exception. * What went wrong: 'java.io.File org.gradle.api.reporting.ConfigurableReport.getDestination()'
通过gradle build clean --scan排查,确认错误源于name.remal.sonarlint插件调用了Gradle 8.0中已移除的org.gradle.api.reporting.ConfigurableReport.getDestination()方法。回退至Gradle 7.6时,系统提示Report.destination、Report.enabled、AbstractArchiveTask.archiveName等属性已弃用,这些属性在Gradle 8.0中已被彻底移除。
解决步骤
1. 升级不兼容插件
name.remal.sonarlint 1.5.0版本不支持Gradle 8.x,需升级到兼容版本(建议使用2.0+版本):
id("name.remal.sonarlint") version "2.1.0" // 替换为最新兼容版本
2. 替换已移除的弃用属性
Gradle 8.0对多个旧API做了移除,需替换为新的配置方式:
archiveName→archiveFileName:所有Jar/War/Ear任务中的归档文件名配置需替换Report.destination→Report.outputLocation:修改Checkstyle等报告的输出路径配置Report.enabled→Report.required:调整报告的启用逻辑
3. 同步其他插件兼容性
确保项目中其他插件版本适配Gradle 8.0:
com.github.spotbugs建议升级至5.1.3+org.sonarqube建议升级至4.0.0+
修改后的关键配置片段
插件版本更新
plugins { id("war") id("ear") id("idea") id("java-library") id("org.jetbrains.kotlin.jvm") version "1.8.10" id("checkstyle") id("pmd") id("com.github.spotbugs") version "5.1.3" id("org.sonarqube") version "4.0.0.2929" id("name.remal.sonarlint") version "2.1.0" id("org.wildfly.build.provision") version "0.0.11" }
报告配置调整
tasks.withType(Checkstyle) { reports { html.outputLocation = rootProject.layout.buildDirectory.file("reports/checkstyle/checkstyle.html") } } tasks.withType(Pmd) { reports { xml.required = true html.required = true } }
归档任务文件名修改
task NOTiFYmotoWAR(type: War) { // ... 其他配置保持不变 archiveFileName = "NOTiFYmotoWAR.war" } task NOTiFYmotoJAR(type: Jar) { // ... 其他配置保持不变 archiveFileName = "NOTiFYmotoJAR.jar" } task NOTiFYmotoEAR(type: Ear) { // ... 其他配置保持不变 archiveFileName = "NOTiFYmoto.ear" }
SpotBugs配置适配
spotbugs { ignoreFailures = true toolVersion = '4.7.3' // 若reportsDir属性弃用,替换为具体报告的输出路径配置 reports.html.outputLocation = layout.buildDirectory.file("reports/spotbugs/spotbugs.html") reports.xml.outputLocation = layout.buildDirectory.file("reports/spotbugs/spotbugs.xml") effort = "max" reportLevel = "high" showProgress = true }
验证操作
- 执行
gradle clean build --refresh-dependencies刷新依赖并重新构建 - 检查控制台输出,确认无弃用警告或错误
- 若仍有问题,逐一排查剩余插件和依赖的版本兼容性
内容的提问来源于stack exchange,提问作者NOTiFY
相关产品推荐
相关产品推荐

