Heroku部署Java项目构建失败 寻求解决方案
问题分析
错误invalid flag: --release的核心原因是:Heroku构建环境使用的JDK版本低于Java 9,而--release是Java 9及以上版本javac才支持的编译参数。即便你在pom.xml中配置了Java 11和Maven 3.8.1,Heroku仍可能未正确识别配置,默认使用了低版本JDK。
解决步骤
强制指定Heroku的JDK版本
在项目根目录创建system.properties文件,写入以下内容:java.runtime.version=11该文件会直接告知Heroku构建环境使用Java 11,覆盖默认的低版本JDK。
修正pom.xml中的编译插件配置
确保maven-compiler-plugin明确绑定Java 11版本,避免参数冲突:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>11</source> <target>11</target> <release>11</release> </configuration> </plugin>注:
<release>参数仅在Java 9+环境生效,必须配合system.properties的配置才能正常使用。验证构建环境的JDK版本
可在pom.xml中添加预处理步骤,打印JDK版本确认环境是否正确:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>validate</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>java</executable> <arguments> <argument>-version</argument> </arguments> </configuration> </execution> </executions> </plugin>重新推送后,查看构建日志中的JDK版本输出,确认是否为Java 11。
清理Heroku构建缓存
若存在旧缓存导致的环境异常,执行以下命令清理后重新推送:heroku builds:cache:purge -a your-app-name替换
your-app-name为你的Heroku应用名称。
内容的提问来源于stack exchange,提问作者Tuấn Truong
相关产品推荐
相关产品推荐

