如何在Spring/Spring Boot pom.xml中指定Java版本? [英] How to specify Java version in Spring/Spring Boot pom.xml?

查看:33
本文介绍了如何在Spring/Spring Boot pom.xml中指定Java版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Java 11指定为在Spring(或Spring Boot)pom.xml文件中使用的版本的正确方式是什么?

即,我需要在pom的java.version属性中输入什么值?

对于Java 8,我们使用1.8,就像the documentation shows一样,对于Java 9,它是1.9

我不确定Java 11是否会是1.11(尽管它似乎不太可能),我看到它在使用maven-compiler-plugin时只是指定为11,但我没有使用编译器插件。

例如

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

我到处搜索,但都找不到答案,因为我读过的几乎所有文档以及与Java版本相关的博客文章都只显示版本8或9。

那么我应该使用什么?1.11还是只使用11

我两个都试过了,我的Web服务器似乎正确启动和正确编译(IntelliJ显示Information:javac 11.0.2 was used to compile java sources),但我不完全相信更改java.version值有什么作用,因为我可以将其设置为例如100,一切正常。

我能找到的唯一相关问题是:Minimum Spring version compatible with Java 11Spring Boot fails due to a Hibernate error after migrating to JDK 11Spring 4.3.x with OpenJDK 11,但它们并没有真正揭示任何问题。

推荐答案

简短回答:

正确的方法是对不同的Java版本使用<java.version>中的下列值:

  • Java 8:1.8或8
  • Java 9:9
  • Java 10:10
  • Java 11:11
  • Java 12:12
  • .....
  • .....
  • Java 16:16
  • Java 17:17

因此,对于Java 11,应该是:

<properties>
   <java.version>11</java.version>
</properties>
然而,我不确定Java 11是否会是1.11&q;(似乎不太可能),以及 我看到它在使用maven-编译器-plugin时被指定为11&q; 但是,我没有使用编译器插件。

实际上,它最后仍然使用maven-compiler-plugin进行编译。SpringBoot只配置一个<java.version>属性,这样通过更改此值,您就隐式地将maven-compiler-plugin<source/><target/>更改为与<java.version>

中指定的值相同的值
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>11</source>  <!-- same as <java.version> -->
        <target>11</target>    <!-- same as <java.version> -->
    </configuration>
</plugin>

详细答案:

您似乎想要详细信息来说服您。

因为每个Spring Boot项目都会对父POMspring-boot-starter-parentdefines<java.version>进行如下扩展:

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
docsmaven.compiler.sourcemaven.compiler.target<source><target>配置参数的user property。由于用户属性的行为,将这两个属性设置为11意味着设置以下内容:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>11</source>   <!-- maven.compiler.source  -->
        <target>11</target> <!-- maven.compiler.target -->
    </configuration>
</plugin>
maven-compiler-plugindocs中,<source><target>是Java编译器(javac)的-source-target参数。然后,从javac文档中,我们可以看到这两个参数被允许具有以下值:

  • 1.6:Java SE 6中没有引入任何语言更改。但是,源文件中的编码错误现在报告为错误,而不是 警告与早期版本的Java Platform标准版相同。
  • 6:1.6的同义词。
  • 1.7:编译器接受具有Java SE 7中引入的功能的代码。
  • 7:1.7的同义词。
  • 1.8:编译器接受具有Java SE 8中引入的功能的代码。
  • 8:1.8的同义词。
  • 9:编译器接受具有Java SE 9中引入的功能的代码。
  • 10:编译器接受具有Java SE 10中引入的功能的代码。
  • 11:编译器接受具有Java SE 11中引入的功能的代码。
  • 12:编译器接受具有Java SE 12中引入的功能的代码。

因此,对于Java 11,<java.version>应设置为11

这篇关于如何在Spring/Spring Boot pom.xml中指定Java版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆