解决:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin

本文记录了使用IDEA进行Maven打包时遇到的错误及其解决过程。主要问题是由于JDK环境变量配置不当导致的编译失败。通过调整JAVA_HOME环境变量指向正确的JDK路径,最终解决了编译问题。

1.mvn clean install -DskipTests; idea terminal 打包报错(用idea右边maven插件打包是没有问题的)。

[INFO] BUILD FAILURE

[INFO] ------------------------------------------------------------------------

[INFO] Total time:  1.095 s

[INFO] Finished at: 2022-03-30T03:40:49+08:00

[INFO] ------------------------------------------------------------------------

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project spiInterface: Compilation failure -> [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/MojoFailureException

2.开了-X,报错如下:

命令行mvn打包的时候报错:No compiler is provided in this environment. Perhaps you are running on a JRE

3.定位到根因,是jdk环境变量配置的问题

 默认的java_home 里面只有jre,所以无法编译。

我的mac有两个版本的java 1.8.0.311和1.8.0.312,这里默认配置1.8识别到的是前者

export JAVA_8_HOME="$(/usr/libexec/java_home -v 1.8)"
export JAVA_17_HOME="$(/usr/libexec/java_home -v 17)"

#默认设为JDK1.8
export JAVA_HOME=$JAVA_8_HOME

#alias命令动态切换JDK版本
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk17="export JAVA_HOME=$JAVA_17_HOME"
xukunpeng@192 ~ % java -version 
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)

4.所以修改配置如下,地址更新到下面这个:

5.source ~/.bash_profile 一下 ,让其生效

xukunpeng@192 ~ % java -version                                                                                        
openjdk version "1.8.0_312"
OpenJDK Runtime Environment Corretto-8.312.07.1 (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM Corretto-8.312.07.1 (build 25.312-b07, mixed mode)

7.restart idea,必要步骤

  

8.mvn clean install -DskipTests;  不出意外的build成功。

 

 

### 解决 Maven 编译插件执行失败的方法 当遇到 `Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile` 错误时,通常是因为编译器配置不正确或环境设置有问题。以下是详细的解决方案: #### 配置 JDK 版本一致性 确保本地使用的 Maven 和 IDE 中的 JDK 版本一致非常重要。如果 IDEA 使用的是 JDK 17 而 Maven 插件版本较低,则可能导致兼容性问题[^3]。 ```xml <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> ``` #### 更新 maven-compiler-plugin 到最新版 旧版本的 `maven-compiler-plugin` 可能存在一些已知的问题,建议升级到最新的稳定版本以获得更好的支持和修复。 ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <!-- 或者更高 --> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> ``` #### 设置正确的 JRE 安装路径 确认 Eclipse/IDEA 的首选项里已经选择了合适的 JDK 安装目录而非 JRE,并且该路径指向了一个完整的开发工具包而不是运行时环境[^4]。 #### 清理并重新构建项目 有时候缓存文件可能会引起冲突,在命令行下尝试清理后再进行打包操作可以排除此类干扰因素。 ```bash mvn clean install ``` 通过以上措施应该能够有效解决大多数情况下由于 `maven-compiler-plugin` 导致的目标无法执行的问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值