IntelliJ IDEA刷新SpringBoot项目提示Gradle依赖缓存损坏报错
问题描述
使用IntelliJ IDEA 2022.1.1 (Ultimate Edition)刷新Spring Boot Gradle项目时,抛出如下错误:
Unable to find method ''org.gradle.api.tasks.SourceSetContainer org.gradle.api.plugins.JavaPluginExtension.getSourceSets()'' 'org.gradle.api.tasks.SourceSetContainer org.gradle.api.plugins.JavaPluginExtension.getSourceSets()' Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network) The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build processes (requires restart) Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project. In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
已尝试以下操作均未解决问题:
- 重启IDE
- 执行Invalidate Caches操作后重启IDE
- 停止Gradle构建进程后重启IDE
- 删除
./gradle/wrapper/dist目录 - 修改
gradle/wrapper目录下配置的Gradle版本
但在终端执行命令./gradlew clean :dolphin-post:dolphin-post-service:build -x test可以正常构建成功,构建使用的Gradle版本为7.4.1,最终输出BUILD SUCCESSFUL。
问题根因
这个报错和Gradle缓存损坏、守护进程异常没有关系,核心问题是IntelliJ IDEA 同步项目时调用的Gradle运行时版本,和项目要求的Gradle版本不匹配。
终端执行./gradlew命令能正常构建,已经证明项目本身的构建脚本、依赖、wrapper配置全是正常的。报错里提到的org.gradle.api.plugins.JavaPluginExtension.getSourceSets()是Gradle 7.1及以上版本才提供的公共API,IDEA同步时如果用了低于7.1的Gradle版本,就会抛出方法不存在的错误。
之前尝试的重启、清缓存、删wrapper下载目录、修改wrapper配置版本这些操作都没生效,是因为这些操作都没有修改IDEA同步Gradle项目时的核心运行时配置——IDEA默认可能调用自带的嵌入式低版本Gradle做同步,根本不会读取项目里的wrapper配置。
修复方案
按顺序操作,同步成功即可停止:
- 打开IDEA设置,定位到
Build, Execution, Deployment > Build Tools > Gradle配置页 - 找到
Use Gradle from配置项,不要选Embedded Gradle(嵌入式Gradle),优先选择Gradle Wrapper选项,同时把下方的Gradle JVM配置成和终端构建项目用的JDK完全一致的版本,不要用IDEA自带的运行时JBR - 如果选了
Gradle Wrapper还是报错,就手动指定Gradle路径:终端第一次构建已经自动下载好了Gradle 7.4.1,默认存放在用户目录下的.gradle/wrapper/dists/gradle-7.4.1-all/路径下,找到解压完成的Gradle根目录,把Use Gradle from改成Specified location,路径填这个解压后的Gradle目录 - 点击Apply保存配置后,点击IDEA右侧Gradle面板的刷新按钮重新同步项目即可
- 如果以上操作都无效,说明是IDEA 2022.1.1自带的Spring Boot插件的已知兼容问题:这个版本的IDEA对Spring Boot 3.x配套的Gradle插件适配有缺陷,要么把IDEA升级到2022.2及以上版本,要么把项目的Spring Boot Gradle插件版本降到2.7.x分支即可。
内容的提问来源于stack exchange,提问作者Dolphin
相关产品推荐
相关产品推荐

