Spring应用部署Heroku失败:Build Failure错误排查求助
Heroku部署Spring应用构建失败问题解决
问题描述
Heroku是一款平台即服务(PaaS),支持开发者完全在云端构建、运行和管理应用。我的Spring应用在本地运行正常,但部署到Heroku时出现构建失败(Build Failure)错误,请求协助解决。
错误日志
[INFO] Changes detected - recompiling the module! [INFO] Compiling 25 source files to /tmp/build_d30a8927/target/classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 8.201 s [INFO] Finished at: 2022-10-14T18:42:07Z [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project MyApp: Fatal error compiling: invalid target release: 17 -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException ! ERROR: Failed to build app with Maven 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/ ! Push rejected, failed to compile Java app. ! Push failed
项目POM配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.spring.demo</groupId> <artifactId>MyApp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MyApp</name> <description>Demo project for MyApp</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
解决方案
核心问题是Heroku默认使用的Java版本低于项目指定的Java 17,导致编译时抛出invalid target release: 17错误,以下是两种可行的解决方式:
方式一:通过system.properties指定Java版本
在项目根目录创建system.properties文件,添加以下内容:
java.runtime.version=17
将该文件提交至Git仓库后重新部署,Heroku会自动使用Java 17环境进行构建。
方式二:在pom.xml中明确配置Maven编译插件
在pom.xml的<build>节点下的<plugins>中添加maven-compiler-plugin的版本及Java版本配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin>
建议同时结合方式一的system.properties配置,确保Heroku的运行和构建环境都使用Java 17,避免版本不一致问题。
注:Spring Boot 2.7.3原生支持Java 17,无需调整Spring Boot版本。
内容的提问来源于stack exchange,提问作者harry
相关产品推荐
相关产品推荐

