Maven编译报错invalid flag: --release,SpringBoot部署Heroku遇阻
解决SpringBoot部署Heroku时Maven编译报错
invalid flag: --release 报错信息
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project Blinknotes: Fatal error compiling: invalid flag: --release -> [Help 1]
完整错误输出:
remote: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project Blinknotes: Fatal error compiling: invalid flag: --release -> [Help 1] remote: [ERROR] remote: [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. remote: [ERROR] Re-run Maven using the -X switch to enable full debug logging. remote: [ERROR] remote: [ERROR] For more information about the errors and possible solutions, please read the following articles remote: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
当前pom.xml配置
<?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>3.2.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.rajbhut.blog</groupId> <artifactId>Blinknotes</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Blinknotes</name> <description>this is backend apis for notes app</description> <properties> <java.version>19</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.cloudinary</groupId> <artifactId>cloudinary-http44</artifactId> <version>1.36.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>19</source> <target>19</target> </configuration> </plugin> </plugins> </build> </project>
已尝试的操作
- 验证JAVA_HOME和PATH指向正确的JDK 19安装
- 执行
mvn clean install及mvn clean install -X - 执行
mvn dependency:purge-local-repository清理本地仓库
解决方案
核心原因
Heroku默认使用的JDK版本可能低于19,同时手动指定的maven-compiler-plugin版本与Spring Boot父POM的默认配置冲突,导致生成了低版本JDK不支持的--release编译参数。
具体修复步骤
移除手动配置的maven-compiler-plugin
Spring Boot的spring-boot-starter-parent已经内置了适配当前Spring Boot版本的maven-compiler-plugin,会自动根据java.version配置编译参数,手动添加插件会引发版本冲突。修改后的build部分如下:<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>指定Heroku使用的JDK版本
在项目根目录创建system.properties文件,写入以下内容:java.runtime.version=19该文件会告诉Heroku构建时使用JDK 19,避免默认版本过低导致的参数不兼容问题。
重新部署
执行以下命令提交修改并重新部署:git add . git commit -m "Fix maven compiler conflict and specify JDK 19 for Heroku" git push heroku main
内容的提问来源于stack exchange,提问作者Raj Bhut
相关产品推荐
相关产品推荐

