Spring Boot 3.0.1升级报错:无法解析spring-boot-gradle-plugin
问题
将Kotlin项目从Spring Boot 2.7.x升级至3.0.1,使用Java 17 Temurin、Gradle 7.6版本。在IntelliJ中通过Gradle导入项目时,出现如下错误:
A problem occurred configuring root project 'demo'. > Could not resolve all files for configuration ':classpath'. > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.1. Required by: project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.1 > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.1 was found. The consumer was configured to find a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but: - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.1 declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares an API of a component compatible with Java 17 and the consumer needed a runtime of a component compatible with Java 11 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.1 declares a runtime of a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component compatible with Java 17 and the consumer needed a component compatible with Java 11 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a runtime of a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component compatible with Java 17 and the consumer needed a component compatible with Java 11 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.6') - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.1 declares a runtime of a component, and its dependencies declared externally: - Incompatible because this component declares documentation and the consumer needed a library - Other compatible attributes: - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about its elements (required them packaged as a jar) - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
从start.spring.io下载了同类项目骨架,其build.gradle.kts配置如下:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id("org.springframework.boot") version "3.0.1" id("io.spring.dependency-management") version "1.1.0" kotlin("jvm") version "1.7.22" kotlin("plugin.spring") version "1.7.22" } group = "com.example" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_17 repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boot-starter") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") testImplementation("org.springframework.boot:spring-boot-starter-test") } tasks.withType<KotlinCompile> { kotlinOptions { freeCompilerArgs = listOf("-Xjsr305=strict") jvmTarget = "17" } } tasks.withType<Test> { useJUnitPlatform() }
导入该项目时仍出现相同错误,求解决思路。
解决思路
错误核心是:Gradle运行时使用的是Java 11,但Spring Boot 3.0.x的Gradle插件要求运行环境必须是Java 17。项目配置的java.sourceCompatibility只是编译版本,不影响Gradle daemon的运行JVM版本。
按以下步骤修复:
调整IntelliJ的Gradle JVM配置
- 打开
Settings(Mac下为Preferences)→Build, Execution, Deployment→Build Tools→Gradle - 在
Gradle JVM下拉框中选择已安装的Java 17 Temurin - 点击
Apply保存,然后重新同步Gradle项目
- 打开
通过
gradle.properties强制指定Gradle运行JVM
在项目根目录创建或修改gradle.properties文件,添加:org.gradle.java.home=/path/to/your/java17/temurin替换路径为本地Java 17 Temurin的实际安装目录(比如Windows:
C:\Program Files\Eclipse Adoptium\jdk-17.0.8.101-hotspot,Mac:/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home)检查系统
JAVA_HOME环境变量- 确保系统级
JAVA_HOME指向Java 17 Temurin - 重启IntelliJ后重新导入项目,避免环境变量未生效
- 确保系统级
验证Gradle版本兼容性
Spring Boot 3.0.x支持Gradle 7.5及以上,当前7.6版本符合要求。若以上步骤无效,可尝试升级Gradle到7.6.1或8.x版本。
内容的提问来源于stack exchange,提问作者Roman T

