SpringBoot(Gradle) Web应用终端启动构建失败求助
SpringBoot Gradle构建失败问题解决
问题详情
执行./gradlew bootRun命令运行SpringBoot Web应用时,构建失败,错误信息如下:
FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'topic1-1'. > Could not resolve all artifacts for configuration ':classpath'. > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.2.5. Required by: project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.2.5 > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.2.5 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.7' but: - Variant 'apiElements' declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.7') - Variant 'javadocElements' declares a component for use during runtime, 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 elements (required them packaged as a jar) - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about org.gradle.plugin.api-version (required '8.7') - Variant 'mavenOptionalApiElements' declares a library, packaged as a jar, and its dependencies declared externally: - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '8.7') - Variant 'mavenOptionalRuntimeElements' declares a library for use during runtime, 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 '8.7') - Variant 'runtimeElements' declares a library for use during runtime, 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 '8.7') - Variant 'sourcesElements' declares a component for use during runtime, 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 elements (required them packaged as a jar) - Doesn't say anything about its target Java version (required compatibility with Java 11) - Doesn't say anything about org.gradle.plugin.api-version (required '8.7') * Try: > Review the variant matching algorithm in Gradle documentation. > No matching variant errors are explained in more detail in Gradle documentation. > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at Gradle help center. BUILD FAILED in 700ms
环境配置
- build.gradle文件:
plugins { id 'java' id 'org.springframework.boot' version '3.2.5' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.example' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' developmentOnly 'org.springframework.boot:spring-boot-devtools' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } tasks.named('test') { useJUnitPlatform() }
- Java版本:
openjdk version "11.0.22" 2024-01-16 OpenJDK Runtime Environment (build 11.0.22+7-post-Ubuntu-0ubuntu222.04.1) OpenJDK 64-Bit Server VM (build 11.0.22+7-post-Ubuntu-0ubuntu222.04.1, mixed mode, sharing)
解决方案
问题本质是Spring Boot 3.2.5要求Java 17及以上版本,但当前运行Gradle的JDK是11,同时build.gradle里配置的sourceCompatibility为17,两者不匹配导致插件无法解析。
提供两种可行解决方式:
方式一:升级本地Java版本到17
安装OpenJDK 17或Oracle JDK 17,配置环境变量JAVA_HOME指向JDK 17目录,并确保PATH优先加载该JDK的bin目录。验证安装成功:
java -version
输出显示Java 17版本后,重新执行./gradlew bootRun即可正常构建。
方式二:降级Spring Boot版本到兼容Java 11的版本
Spring Boot 3.x系列最低要求Java 17,若不想升级Java,可降级到2.7.x系列(最高2.7.18,支持Java 8-17)。修改build.gradle如下:
plugins { id 'java' id 'org.springframework.boot' version '2.7.18' id 'io.spring.dependency-management' version '1.0.15.RELEASE' } java { sourceCompatibility = '11' }
同步Gradle配置后,重新执行./gradlew bootRun即可构建成功。
内容的提问来源于stack exchange,提问作者marcelo gallardo
相关产品推荐
相关产品推荐

