如何解决Spring Boot 3.0.0部署Heroku时spring-boot-gradle-plugin兼容问题
Spring Boot 3.0.0(Java 17)部署Heroku构建失败问题解决
问题详情
使用GitLab CI/CD将基于Java 17的Spring Boot 3.0.0应用部署到Heroku时,构建失败。核心原因是Heroku默认安装了OpenJDK 1.8,但Spring Boot 3.0.0的Gradle插件仅兼容Java 17,导致无法解析org.springframework.boot:spring-boot-gradle-plugin:3.0.0依赖。
错误日志
Deploying application uploading application archive triggering new deployment -----> Building on the Heroku-22 stack -----> Determining which buildpack to use for this app -----> Gradle app detected -----> Spring Boot detected -----> Installing OpenJDK 1.8... done -----> Building Gradle app... -----> executing ./gradlew build -x check Downloading https://services.gradle.org/distributions/gradle-7.5.1-bin.zip ...........10%............20%...........30%............40%...........50%............60%...........70%............80%...........90%............100% To honour the JVM settings for this build a single-use Daemon process will be forked. See https://docs.gradle.org/7.5.1/userguide/gradle_daemon.html#sec:disabling_the_daemon. Daemon will be stopped at the end of the build FAILURE: Build failed with an exception. * What went wrong: A problem occurred configuring root project 'auth'. > Could not resolve all files for configuration ':classpath'. > 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 8, 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 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1') - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.0 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 8) - 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.5.1') - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional: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 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1') - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.0.0 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 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1') - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.0 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 8 - Other compatible attribute: - Doesn't say anything about org.gradle.plugin.api-version (required '7.5.1') - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.0.0 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 8) - 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.5.1') * Try: > 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 https://help.gradle.org BUILD FAILED in 9s ! ERROR: Failed to run Gradle! We're sorry this build is failing. If you can't find the issue in application code, please submit a ticket so we can help: https://help.heroku.com You can also try reverting to the previous version of the buildpack by running: $ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-gradle#previous-version Thanks, Heroku
解决方案
- 指定Heroku使用Java 17:在项目根目录创建
system.properties文件,添加以下内容:
该文件会告知Heroku构建环境使用Java 17。java.runtime.version=17 - 配置Gradle的Java版本:在
build.gradle中明确指定Java版本为17,确保与Spring Boot 3.0.0兼容:
同时确认Spring Boot插件版本正确:java { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 }plugins { id 'org.springframework.boot' version '3.0.0' id 'io.spring.dependency-management' version '1.1.0' id 'java' } - 检查GitLab CI/CD配置:确保
.gitlab-ci.yml中没有强制设置Java 8的指令,部署流程正常调用Heroku部署即可。
内容的提问来源于stack exchange,提问作者noBoom
相关产品推荐
相关产品推荐

