You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署集成MySQL的Spring Boot到Heroku时Gradle构建失败

问题:Spring Boot 3.1.0 部署到Heroku构建失败

本地开发的Spring Boot应用已接近部署就绪,尝试部署到Heroku平台,但推送代码后执行./gradlew build -x check步骤时构建失败,该命令在本地运行完全正常。不清楚Heroku环境下应用需要如何配置,附上相关信息:

构建日志

-----> 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.6.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.6.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 'RnR'.
       > Could not resolve all files for configuration ':classpath'.
          > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.1.0.
            Required by:
                project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.1.0
             > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.1.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.6.1' but:
                 - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.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.6.1')
                 - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.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.6.1')
                 - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.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.6.1')
                 - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.1.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.6.1')
                 - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.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.6.1')
                 - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.1.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.6.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 8s
 !     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
 !     Push rejected, failed to compile Gradle app.
 !     Push failed

build.gradle 文件

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'checkstyle'
}

group = 'FamilyFusion'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // Spring Boot Starters
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    // Validation
    implementation 'org.springframework.boot:spring-boot-starter-validation:3.0.6'

    // Thymeleaf Extras
    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
    implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.2.1'

    // File Upload
    implementation 'commons-fileupload:commons-fileupload:1.5'

    // Gedcom4j
    implementation 'org.gedcom4j:gedcom4j:4.0.1'

    // JSON
    implementation 'org.json:json:20230618'

    // Lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    // Development Tools
    developmentOnly 'org.springframework.boot:spring-boot-devtools'

    // Database Connector
    runtimeOnly 'com.mysql:mysql-connector-j'

    // Testing
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

checkstyle {
    toolVersion = 8.40
    maxWarnings = 0
    ignoreFailures = true
}

tasks.withType(Checkstyle).configureEach {
    reports {
        xml.required = true
        html.required = true
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

application.properties 文件

####    Database     ####
spring.datasource.url=jdbc:mysql://localhost:3306/roots_and_recipes
spring.datasource.username=*****
spring.datasource.password=******
spring.jpa.database-platform=MySQL
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false

####    SSL     ####
server.port=8443
server.ssl.enabled=true
server.ssl.key-store=classpath:FamilyFusionSecurity.p12
server.ssl.key-store-password=******
server.ssl.key-store-type=PKCS12
server.ssl.key-alias=FamilyFusionSecurity
server.ssl.protocol=TLS

####    Spring Mail     ####
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=****
spring.mail.password=******
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

已知当前数据库配置为本地,打算先解决构建问题,之后再连接JawsDB MySQL免费版云数据库。


解决方案

核心问题

构建失败的原因明确:Heroku默认安装OpenJDK 1.8,但Spring Boot 3.1.0要求Java 17(build.gradle中sourceCompatibility = '17'也能佐证),版本不兼容导致无法解析Spring Boot Gradle插件。

解决步骤

  1. 指定Heroku使用Java 17
    在项目根目录创建system.properties文件,内容如下:

    java.runtime.version=17
    

    该文件会告知Heroku构建时安装Java 17,而非默认的Java 8。

  2. 优化配置(可选但建议)

    • 统一依赖版本:spring-boot-starter-validation版本3.0.6与Spring Boot 3.1.0不一致,建议改为implementation 'org.springframework.boot:spring-boot-starter-validation',让依赖管理插件自动匹配版本。
    • 适配Heroku端口:移除server.port=8443,改为server.port=${PORT},使用Heroku提供的端口环境变量。
    • 移除本地SSL配置:Heroku会自动提供SSL证书,无需本地配置,可删除相关SSL参数。
    • 后续数据库配置:连接JawsDB时,替换数据库配置为环境变量:
      spring.datasource.url=${JAWSDB_URL}
      spring.datasource.username=${JAWSDB_USERNAME}
      spring.datasource.password=${JAWSDB_PASSWORD}
      
  3. 重新推送代码
    将system.properties加入Git并推送:

    git add system.properties
    git commit -m "指定Heroku使用Java 17"
    git push heroku main
    

内容的提问来源于stack exchange,提问作者namer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 18:35:56