使用Spring Boot Gradle Plugin 3.0.0出现无匹配变体错误
Spring Boot Gradle Plugin 3.0.0 依赖解析错误:Java版本不兼容
问题场景
在IntelliJ中使用Spring Boot Gradle Plugin 3.0.0时出现依赖解析失败,切换至2.7.6版本后错误消失。项目的build.gradle.kts配置如下:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { id("org.springframework.boot") version "3.0.0" id("io.spring.dependency-management") version "1.1.0" kotlin("jvm") version "1.7.20" kotlin("plugin.spring") version "1.7.20" id("org.jlleitschuh.gradle.ktlint") version "11.0.0" } java.sourceCompatibility = JavaVersion.VERSION_17 repositories { mavenCentral() } dependencies { implementation("org.springframework.boot:spring-boot-starter-data-r2dbc") implementation("org.springframework.boot:spring-boot-starter-webflux") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("io.projectreactor.kotlin:reactor-kotlin-extensions") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") runtimeOnly("org.postgresql:postgresql") runtimeOnly("org.postgresql:r2dbc-postgresql") testImplementation("org.springframework.boot:spring-boot-starter-test") testImplementation("io.projectreactor:reactor-test") jdbc("org.postgresql:postgresql:42.5.0") } tasks.withType<KotlinCompile> { kotlinOptions { freeCompilerArgs = listOf("-Xjsr305=strict") jvmTarget = "17" } } tasks.withType<Test> { useJUnitPlatform() }
错误信息
> Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.0.0. Required by: project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.0.0 > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.0.0 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.5.1' but: - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.0 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.5.1')
核心原因
Spring Boot Gradle Plugin 3.0.0要求构建环境必须使用Java 17,虽然项目配置中java.sourceCompatibility设为17,但Gradle实际运行时使用的是Java 11,导致插件版本与运行环境不兼容。
解决方案
1. 检查Gradle运行时的Java版本
执行以下命令确认当前Gradle使用的JVM版本:
./gradlew --version
查看输出中的JVM:字段,确保版本为17或更高。
2. 配置IntelliJ使用Java 17作为Gradle JVM
- 打开IntelliJ的
Settings(Windows/Linux:File -> Settings;Mac:IntelliJ IDEA -> Settings) - 导航到
Build, Execution, Deployment -> Build Tools -> Gradle - 在
Gradle JVM下拉菜单中选择已安装的Java 17 SDK,点击Apply保存。
3. 确认项目SDK配置
- 打开
Project Structure(快捷键:Ctrl+Alt+Shift+S) - 进入
Project Settings -> Project - 设置
Project SDK为Java 17,Project language level设为17 - Sealed classes, pattern matching,点击OK保存。
完成以上配置后,重新同步Gradle项目即可解决依赖解析错误。
内容的提问来源于stack exchange,提问作者die Wolken
相关产品推荐
相关产品推荐

