背景
进行maven install 命令,报错:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.310 s
[INFO] Finished at: 2024-11-26T11:22:34+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project channelapi-service: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR]
[ERROR] -> [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
分析
java安装正常
>java -versionjava version "1.8.0_381"
Java(TM) SE Runtime Environment (build 1.8.0_381-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.381-b09, mixed mode)>/usr/libexec/java_home -VMatching Java Virtual Machines (3):1.8.381.09 (arm64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home1.8.0_381 (arm64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk-1.8.jdk/Contents/Home1.8.0_202 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
mvn -v 查看 runtime 环境
mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/manka/software/apache-maven-3.6.3
Java version: 1.8.0_381, vendor: Oracle Corporation, runtime: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "14.6.1", arch: "aarch64", family: "mac"
解决
删除掉 JavaAppletPlugin.plugin 这个插件即可。
进入 /Library/Internet Plug-Ins/ 目录,删除 /Library/Internet Plug-Ins/JavaAppletPlugin.plugin
> rm -rf JavaAppletPlugin.plugin
重新执行 mvn -v runtime 正确
mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/manka/software/apache-maven-3.6.3
Java version: 1.8.0_381, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-1.8.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "14.6.1", arch: "aarch64", family: "mac"
补充知识
MAC OS X环境的所有配置以及加载顺序如下:
# 系统级别
/etc/profile
/etc/paths # 用户级别
~/.bash_profile
~/.bash_login
~/.profile ~/.bashrc(或者~/.zshrc)
前两个环境配置在系统启动时候就会加载,针对所有用户生效,后面四个属于具体用户级别的配置。
Maven会按照以下顺序来确定使用哪个JDK:
-
Maven配置:Maven允许你通过
maven-compiler-plugin
插件指定使用的JDK版本。如果你在项目的pom.xml
文件中配置了这个插件,并指定了JDK版本,Maven会尝试使用这个版本。 -
环境变量:如果Maven配置中没有指定JDK版本,它会查找环境变量
JAVA_HOME
。如果JAVA_HOME
被设置,Maven会使用这个环境变量指向的JDK版本。 -
系统路径:如果
JAVA_HOME
没有被设置,Maven会退回到系统路径(PATH
环境变量)来查找java
和javac
命令。系统路径中列出的第一个java
命令将被使用。 -
JDK安装:如果系统上安装了多个版本的JDK,并且
PATH
环境变量中包含了多个JDK的bin
目录,那么系统会根据PATH
中目录的顺序来决定使用哪个版本的java
命令。 -
系统默认JDK:如果以上都没有指定,Maven将使用系统默认的JDK版本,这通常是操作系统安装时附带的JDK版本。