简体   繁体   English

Maven 使用了错误的 java 版本

[英]Maven is using wrong java version

I am trying to build a maven project on centOS 8. I want maven to use Java version 15. When I run mvn package I get the following error: I am trying to build a maven project on centOS 8. I want maven to use Java version 15. When I run mvn package I get the following error:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile 
(default-compile) on project systembrett-backend: Fatal error compiling: invalid target release: 15 -> [Help 1]

So I suspect that maven is using the wrong Java version, because when i do mvn package -X for debug logs it start with:所以我怀疑 maven 使用了错误的 Java 版本,因为当我执行mvn package -X调试日志时,它以:

Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_275, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.275.b01-1.el8_3.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"

so it looks like, maven is using Java version 1.8.所以看起来,maven 使用的是 Java 版本 1.8。 But mvn -version says:但是mvn -version说:

Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 15.0.2, vendor: AdoptOpenJDK, runtime: /opt/jdk-15.0.2+7
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-240.1.1.el8_3.x86_64", arch: "amd64", family: "unix"

JAVA_HOME is /opt/jdk-15.0.2+7 and PATH is /opt/jdk-15.0.2+7/bin:/home/username/.local/bin:/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin . JAVA_HOME/opt/jdk-15.0.2+7并且PATH/opt/jdk-15.0.2+7/bin:/home/username/.local/bin:/home/username/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

I thought maven is choosing the java version by checking JAVA_HOME , but apparently it is using an other version for the build.我认为 maven 正在通过检查JAVA_HOME选择 java 版本,但显然它正在使用其他版本进行构建。 Does anyone know how to tell maven the correct version?有谁知道如何告诉 maven 正确的版本? Thanks!谢谢!

Following is a list of steps I use to troubleshoot this kind of issues:以下是我用来解决此类问题的步骤列表:

  1. Linux usually works with alternatives to ensure proper default Java environment is used. Linux 通常与alternatives一起使用,以确保使用正确的默认 Java 环境。 Similar to:如同:
$ alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
 + 1           java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.191-2.6.15.4.el7_5.x86_64/jre/bin/java)
*  2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64/jre/bin/java)

Try also for javac as it may not be configured the same way:也可以试试javac ,因为它的配置方式可能不同:

$ alternatives --config javac
  1. For your maven instance, JAVA_HOME should be enough.对于您的maven实例, JAVA_HOME应该足够了。

Your output of mvn -version testifies that you have configured it correctly.您的mvn -version的 output 证明您已正确配置它。 Remove your Java from your PATH to ensure JAVA_HOME and mvn will find the correct one.从您的PATH中删除您的 Java 以确保JAVA_HOMEmvn会找到正确的。

  1. The pom.xml can also configure the required compiler, similar to: pom.xml也可以配置需要的编译器,类似:
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
  1. Another thing may be that you're not using the same ENV (different terminals may not have the same environment variables exported - I presume you being on CentOS, you already encountered this).另一件事可能是您没有使用相同的 ENV(不同的终端可能没有导出相同的环境变量 - 我假设您在 CentOS 上,您已经遇到过这个)。 You have to exit the terminal and get back in to allow the default variables to take effect.您必须退出终端并重新进入以使默认变量生效。

You encounter this usually when different JRE vs JDK are used ( more: maven installation has runtime as JRE instead of JDK )当使用不同的 JRE 和 JDK 时,您通常会遇到这种情况( 更多:maven 安装运行时为 JRE 而不是 JDK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM