Why Maven Uses a Different JDK – 为什么Maven使用不同的JDK

最后修改: 2021年 12月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll explain why Maven might use a different version of Java than the default one set in the system. Additionally, we’ll show where Maven’s configuration files are located. Then, we’ll explain how to configure the Java version in Maven.

在本教程中,我们将解释为什么Maven可能使用与系统中默认设置不同的Java版本。此外,我们还将展示Maven的配置文件的位置。然后,我们将解释如何在Maven中配置Java版本。

2. Maven Configuration

2.Maven配置

First, let’s have a look at a possible system configuration, where Maven uses a different version of Java than the default one set in the system. The Maven configuration returns:

首先,让我们看看一个可能的系统配置,其中Maven使用的Java版本与系统中设置的默认版本不同。Maven配置返回。

$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: C:\Users\test\apps\maven\3.3.9
Java version: 11.0.10, vendor: Oracle Corporation
Java home: C:\my\java\jdk-11.0.10
Default locale: pl_PL, platform encoding: Cp1250
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

As we can see, it returns the Maven version, Java version, and OS information. The Maven tool uses JDK version 11.0.10.

我们可以看到,它返回Maven版本、Java版本和操作系统信息。Maven工具使用JDK 11.0.10版本。

Let’s now have a look at the Java version set in our system:

现在让我们看一下我们系统中的Java版本设置。

$ java -version
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

The default JDK is set to 13.0.2. In the following sections, we’ll explain why it doesn’t match the one used by Maven.

默认的JDK被设置为13.0.2。在接下来的章节中,我们将解释为什么它与Maven使用的不一致。

3. Setting Up JAVA_HOME Globally

3.在全球范围内设置JAVA_HOME

Let’s have a look at the default setup. Above all, the JAVA_HOME variable is a mandatory Maven configuration. Moreover, when it’s not set up, the mvn command returns an error message:

让我们来看看默认设置。首先,JAVA_HOME 变量是Maven的强制配置。此外,当它没有被设置时,mvn命令会返回错误信息。

$ mvn
Error: JAVA_HOME not found in your environment.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

On Linux, we set the system variable with the export command. Windows has dedicated system variable settings for that purpose. When it is set globally, Maven uses the default version of Java set in the system.

在Linux上,我们通过export命令来设置系统变量。Windows有专门的系统变量设置用于此目的。当它被全局设置时,Maven使用系统中设置的Java默认版本。

4. Maven Configuration Files

4.Maven配置文件

Let’s now have a quick look at where to find Maven configuration files. There are a few places where the configuration can be provided:

现在让我们来看看在哪里可以找到Maven的配置文件。有几个地方可以提供配置

  • .mavenrc/mavenrc_pre.cmd – user-defined script located in user’s home directory
  • settings.xml – file located in ~/.m2 directory, which contains configuration across projects
  • .mvn – directory with configuration located within the project

Additionally, we may use MAVEN_OPTS environment variable to set up JVM startup parameters.

此外,我们可以使用MAVEN_OPTS环境变量来设置JVM的启动参数。

5. Setting Up JAVA_HOME Only for Maven

5.只为Maven设置JAVA_HOME

We saw that the Java version differs from the one used by Maven. In other words, Maven overwrites the default configuration provided with the JAVA_HOME variable.

我们看到,Java的版本与Maven使用的版本不同。换句话说,Maven覆盖了JAVA_HOME变量提供的默认配置

It can be set in a user-defined script executed at the beginning of the mvn command. In Windows, we set it in the %HOME%\mavenrc_pre.bat or %HOME%\mavenrc_pre.cmd file. Maven supports both ‘.bat’ and ‘.cmd’ files. In the file, we simply set the JAVA_HOME variable:

它可以在mvn命令开始时执行的用户定义的脚本中设置。在Windows中,我们在%HOME%\mavenrc_pre.bat%HOME%\mavenrc_pre.cmd文件中设置它。Maven同时支持’.bat’和’.cmd’文件。在该文件中,我们只需设置JAVA_HOME变量。

set JAVA_HOME="C:\my\java\jdk-11.0.10"

On the other hand, Linux has the $HOME/.mavenrc file for the same purpose. Here, we set the variable almost the same way:

另一方面,Linux有$HOME/.mavenrc文件用于相同目的。在这里,我们几乎以同样的方式设置变量。

JAVA_HOME=C:/my/java/jdk-11.0.10

Thanks to that setup, Maven uses JDK 11, even though the default one in the system is JDK 13.

由于这一设置,Maven使用了JDK 11,尽管系统中默认的是JDK 13。

We can skip the execution of the user-defined script with the MAVEN_SKIP_RC flag.

我们可以用MAVEN_SKIP_RC标志跳过用户定义的脚本的执行

Additionally, we could set the variable directly in Maven’s executable file. However, such an approach is not recommended as it would not be applied automatically in case we upgrade Maven to a higher version.

此外,我们可以直接在Maven的可执行文件中设置该变量。不过,不建议采用这种方法,因为一旦我们将Maven升级到更高版本,它就不会自动应用。

6. Conclusion

6.结语

In this short article, we explained how Maven might use a different Java version than the default one. Then, we showed where Maven’s configuration files are located. Finally, we explained how to set up the Java version for Maven.

在这篇短文中,我们解释了Maven如何使用与默认版本不同的Java版本。然后,我们展示了Maven的配置文件的位置。最后,我们解释了如何为Maven设置Java版本。