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

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

问题描述

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

What is the correct way of specifying Java 11 as the version to be used in Spring (or Spring Boot) pom.xml file?

也就是说,我需要在pom的java.version属性中输入什么值?

I.e., what value do I need to put in the java.version property of the pom?

对于Java 8,我们使用1.8,就像文档中显示的那样,对于Java 9是1.9.

For Java 8 we use 1.8, just like the documentation shows, for Java 9 it's 1.9.

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

I'm not sure if Java 11 would be 1.11 (although it seems unlikely), and I've seen it specified as just 11 when using maven-compiler-plugin, however I'm not using the compiler plugin.

例如

<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.

I've searched around and couldn't find the answer, as pretty much any documentation I've read as well as blog posts related to the Java version only show versions 8 or 9.

那我该怎么用? 1.11还是11?

So what should I use? 1.11 or just 11?

我同时尝试了两种方法,但我的Web服务器似乎可以正确启动并正确编译(IntelliJ显示Information:javac 11.0.2 was used to compile java sources),但是我并不完全相信更改java.version值可以进行任何操作,因为我可以将其设置为例如100一切正常.

I tried both and my web server seems to start correctly and compile correctly (IntelliJ shows Information:javac 11.0.2 was used to compile java sources) however I'm not entirely convinced that changing the java.version value is doing anything, because I can set it to e.g. 100 and everything works fine.

我能找到的唯一相关问题是:与Java兼容的最低Spring版本11 带有OpenJDK 11的Spring 4.3.x ,但它们并没有真正暴露出任何光辉.

The only related questions I could find are: Minimum Spring version compatible with Java 11, Spring Boot fails due to a Hibernate error after migrating to JDK 11 and Spring 4.3.x with OpenJDK 11, but they don't really shed any light.

推荐答案

简短回答:

正确的方法是将<java.version>中的以下值用于不同的Java版本:

The correct way is to use the followings values in <java.version> for different Java versions:

  • Java 8:1.8或8
  • Java 9:9
  • Java 10:10
  • Java 11:11
  • Java 12:12

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

So for Java 11 , it should be:

<properties>
   <java.version>11</java.version>
</properties>

但是我不确定Java 11是否为"1.11"(似乎不太可能),并且 我已经看到在使用maven-compiler-plugin时将其指定为"11", 但是我没有使用编译器插件.

However I'm not sure if Java 11 would be "1.11" (seems unlikely), and I've seen it specified as just "11" when using maven-compiler-plugin, however I'm not using the compiler plugin.

实际上,最后它仍然使用maven-compiler-plugin进行编译. Springboot只是配置一个<java.version>属性,这样通过更改此值,您可以隐式地将maven-compiler-plugin<source/><target/>更改为与<java.version>中指定的值相同:

Actually , at the end it still uses maven-compiler-plugin to compile. Springboot just configures a <java.version> property such that by changing this value , you are implicitly changing maven-compiler-plugin 's <source/> and <target/> to the same value as what specified in the <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>


详细答案:

好像您想要细节说服您.

Seem like you want details to convince you.

这是因为每个spring boot项目都将扩展父pom spring-boot-starter-parent,其中

It is because every spring boot project will extend the parent pom spring-boot-starter-parent which defines <java.version> as follows:

<properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

来自maven-compiler-plugin 文档maven.compiler.sourcemaven.compiler.target<source>和<target>配置参数的>用户属性.由于用户属性的行为,将这两个属性设置为11意味着要设置以下内容:

From the maven-compiler-plugin docs, maven.compiler.source and maven.compiler.target are the user property for the <source> and <target> config parameters. Due to the behaviour of the user property, setting these two properties to 11 means to set the following :

<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-plugin 文档<source>和<target>是Java编译器(javac)的-source-target参数.然后,从 javac 文档,我们可以看到这两个参数允许具有以下值:

From the maven-compiler-plugin docs again, <source> and <target> are the -source and -target argument for the Java compiler (javac). Then, from javac docs, we can see that these two arguments are allowed to have the following values:

  • 1.6:Java SE 6中未引入语言更改.但是,现在将源文件中的编码错误报告为错误,而不是错误. 警告,就像Java平台标准版的早期版本中所做的一样.
  • 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中引入的功能的代码.
  • 1.6 : No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors instead of warnings as was done in earlier releases of Java Platform, Standard Edition.
  • 6 : Synonym for 1.6.
  • 1.7 : The compiler accepts code with features introduced in Java SE 7.
  • 7 : Synonym for 1.7.
  • 1.8 : The compiler accepts code with features introduced in Java SE 8.
  • 8 : Synonym for 1.8.
  • 9 : The compiler accepts code with features introduced in Java SE 9.
  • 10 : The compiler accepts code with features introduced in Java SE 10.
  • 11 : The compiler accepts code with features introduced in Java SE 11.
  • 12 : The compiler accepts code with features introduced in Java SE 12.

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

Hence, <java.version> should be set to 11 for Java 11.

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

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