Setting the Java Version in Maven – 在Maven中设置Java版本

最后修改: 2018年 10月 13日

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

1. Overview

1.概述

In this quick tutorial, we’ll show how to set the Java version in Maven.

在这个快速教程中,我们将展示如何在Maven中设置Java版本。

Before moving on, we can check the default JDK version of Maven. Running the mvn -v command will show the Java version in which Maven runs.

在继续之前,我们可以检查Maven的默认JDK版本。运行mvn -v命令将显示Maven所运行的Java版本。

2. Use the Compiler Plugin

2.使用编译器插件

We can specify the desired Java version in the compiler plugin.

我们可以在编译器插件中指定需要的Java版本。

2.1. Compiler Plugin

2.1.Compiler Plugin

The first option is setting the version in compiler plugin properties:

第一个选项是在编译器插件属性中设置版本。

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

The Maven compiler accepts this command with –target and –source versions. If we want to use the Java 8 language features, the –source should be set to 1.8.

Maven编译器接受该命令的版本是-target和-source。如果我们想使用Java 8的语言特性,-source应设置为1.8

Also, for the compiled classes to be compatible with JVM 1.8, the –target value should be 1.8.

另外,为了使编译后的类与JVM 1.8兼容,-target值应该是1.8

The default value for both of them is the 1.6 version.

它们的默认值都是1.6版本。

Alternatively, we can configure the compiler plugin directly:

另外,我们也可以直接配置编译器插件。

<plugins>
    <plugin>    
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

The maven-compiler-plugin also has additional configuration properties that allow us to have more control over the compilation process beyond -source and -target versions.

maven-compiler-plugin还有额外的配置属性,使我们能够在-source-target版本之外对编译过程进行更多控制。

2.2. Java 9 and Beyond

2.2.Java 9及其他

Furthermore, starting from the JDK 9 version, we can use a new -release command-line option. This new argument will automatically configure the compiler to produce class files that will link against the implementation of the given platform version.

此外,从JDK 9版本开始,我们可以使用一个新的-release命令行选项。这个新的参数将自动配置编译器,使其产生的类文件能够与给定平台版本的实现相连接。

By default, the -source and -target options don’t guarantee a cross-compilation.

默认情况下,-source-target选项并不保证交叉编译。

This means that we cannot run our application on older versions of the platform. Additionally, to compile and run the programs for older Java versions, we also need to specify -bootclasspath option.

这意味着我们不能在旧版本的平台上运行我们的应用程序。此外,为了编译和运行旧版Java的程序,我们还需要指定-bootclasspath选项。

To cross-compile correctly, the new -release option replaces three flags: -source, -target and -bootclasspath.

为了正确地进行交叉编译,新的-release选项取代了三个标志。-source, -target 和 -bootclasspath

After transforming our examples, we can declare the following for the plugin properties:

在转换了我们的例子之后,我们可以为插件属性声明如下。

<properties>
    <maven.compiler.release>7</maven.compiler.release>
</properties>

And for the maven-compiler-plugin starting from the 3.6 version, this is what we can write:

而对于从3.6版本开始的maven-compiler-plugin,我们可以这样写。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>7</release>
    </configuration>
</plugin>

Notice that we can add the Java version in a new <release> attribute. In this example, we compile our application for Java 7.

注意,我们可以在一个新的<release>属性中添加Java版本。在这个例子中,我们为Java 7编译了我们的应用程序。

What’s more, we don’t need a JDK 7 installed in our machine. Java 9 already contains all the information for linking the new language features with JDK 7.

更重要的是,我们不需要在机器上安装JDK 7。Java 9已经包含了将新的语言特性与JDK 7联系起来的所有信息。

3. Spring Boot Specification

3.Spring Boot规范

Spring Boot applications specify the JDK version inside of the properties tags in the pom.xml file.

Spring Boot应用程序在pom.xml文件中的properties标签内指定JDK版本。

First, we need to add spring-boot-starter-parent as a parent to our project:

首先,我们需要将spring-boot-starter-parent作为父级添加到我们的项目中。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
</parent>

This parent POM allows us to configure default plugins and multiple properties including the Java version — by default, the Java version is 1.8.

这个父级POM允许我们配置默认插件和多个属性,包括Java版本 – 默认情况下,Java版本是1.8

However, we can override the default version of the parent by specifying the java.version property:

然而,我们可以通过指定java.version属性来覆盖父类的默认版本。

<properties>
    <java.version>9</java.version>
</properties>

By setting the java.version property, we declare that the source and the target Java versions are both equal to 1.9.

通过设置java.version属性,我们声明源和目标Java版本都等于1.9

Above all, we should keep in mind that this property is a Spring Boot Specification. Additionally, starting from Spring Boot 2.0, Java 8 is the minimum version.

最重要的是,我们应该记住,这个属性是Spring Boot的规范。此外,从Spring Boot 2.0开始,Java 8是最低版本。

This means we can’t use or configure Spring Boot for the older JDK versions.

这意味着我们不能为旧的JDK版本使用或配置Spring Boot。

4. Conclusion

4.结论

This quick tutorial demonstrated the possible ways of setting Java version in our Maven project.

这个快速教程演示了在Maven项目中设置Java版本的可能方法。

Here’s a summary of the main takeaways:

以下是对主要收获的总结。

  • Using <java.version> is possible only with the Spring Boot application.
  • For simple cases, maven.compiler.source and maven.compiler.target properties should be the best fit.
  • Finally, to have more control over the compilation process, use the maven-compiler-plugin configuration settings.