如何在 Spring/Spring Boot pom.xml 中指定 Java 11 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54467287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:03:04  来源:igfitidea点击:

How to specify Java 11 version in Spring/Spring Boot pom.xml?

javaspringmavenspring-bootpom.xml

提问by Acapulco

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

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

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

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

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

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

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

我不确定 Java 11 是否会1.11(虽然看起来不太可能),而且我已经看到它指定为 just 11when using maven-compiler-plugin,但是我没有使用编译器插件。

e.g.

例如

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

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.

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

So what should I use? 1.11or just 11?

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

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.versionvalue is doing anything, because I can set it to e.g. 100and everything works fine.

我尝试了这两种方法,我的 Web 服务器似乎正确启动并正确编译(IntelliJ 显示Information:javac 11.0.2 was used to compile java sources),但是我并不完全相信更改java.version值会做任何事情,因为我可以将其设置为 eg100并且一切正常。

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 11and Spring 4.3.x with OpenJDK 11, but they don't really shed any light.

我能找到的唯一相关问题是:与 Java 11 兼容的最低 Spring 版本在迁移到 JDK 11Spring 4.3.x 和 OpenJDK 11 后由于 Hibernate 错误Spring Boot 失败,但它们并没有真正说明问题。

采纳答案by Ken Chan

Short Answer:

简答:

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

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

  • Java 8 : 1.8 or 8
  • Java 9 : 9
  • Java 10 : 10
  • Java 11 : 11
  • Java 12 : 12
  • Java 8:1.8 或 8
  • 爪哇 9 : 9
  • 爪哇 10:10
  • 爪哇 11 : 11
  • 爪哇 12 : 12

So for Java 11 , it should be:

所以对于 Java 11 ,它应该是:

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

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.

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

Actually , at the end it still uses maven-compiler-pluginto 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>:

实际上,最后它仍然用于maven-compiler-plugin编译。Springboot 只是配置了一个<java.version>属性,这样通过更改此值,您就隐式地将maven-compiler-plugin's<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>


Detailed Answer:

详细解答:

Seem like you want details to convince you.

似乎你想要细节说服你。

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

这是因为每个 spring boot 项目都会扩展父 pom spring-boot-starter-parent,其定义<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>

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

从Maven的编译器插件的文档maven.compiler.source并且maven.compiler.target用户属性<source><target>配置参数。由于用户属性的行为,将这两个属性设置为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>

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

maven-compiler-plugin文档再次,<source>并且<target>-source-target参数的Java编译器(javac)。然后,从javacdocs 中,我们可以看到这两个参数允许具有以下值:

  • 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.
  • 1.6 :Java SE 6 中没有引入任何语言更改。但是,源文件中的编码错误现在报告为错误,而不是像 Java Platform, Standard Edition 的早期版本那样报告为警告。
  • 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 中引入的特性的代码。

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

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

回答by DwB

You should not need to mention the maven-compiler-plugin in your POM file, if you only want to specify the compiler version. Instead, add these properties to your pom file

如果您只想指定编译器版本,则不需要在 POM 文件中提及 maven-compiler-plugin。相反,将这些属性添加到您的 pom 文件中

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

Edit:My bad, it looks like the version number properties have changed starting with the 3.6 release of the maven-compiler-plugin. The example above is fine for java 8 and lower. For java 9, add this property:

编辑:糟糕,从 maven-compiler-plugin 的 3.6 版本开始,版本号属性似乎发生了变化。上面的示例适用于 java 8 及更低版本。对于 java 9,添加此属性:

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

Check out the accepted answer to this question: Specifying java version in maven - differences between properties and compiler plugin

查看这个问题的公认答案:在 maven 中指定 java 版本 - 属性和编译器插件之间的差异

回答by RKrum

I am adding it as an answer so it can be helpful to more people:

我将其添加为答案,以便对更多人有所帮助:

Since Java 9 the versions are specified as 9, 10, 11, and so on, instead of 1.8, 1.7, etc. Some further reading here.

由于Java 9版本被指定为91011,等等,而不是1.81.7等一些进一步阅读这里

The way you are specifying the JDK version with <release>11</release>is correct. It was introduced with maven-compiler-plugin 3.6and is equivalent as the old way, as below:

您指定 JDK 版本的方式<release>11</release>是正确的。它是用maven-compiler-plugin 3.6和 等价于旧方式引入的,如下所示:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
      <source>11</source> -- Using JDK 11 to compile
      <target>11</target> -- Using JDK 11 as target
    </configuration>
  </plugin>
</plugins>

Thispost can be helpful on using maven-compiler-pluginwith JDK 11.

这篇文章对使用maven-compiler-pluginJDK 11很有帮助。

回答by Ranuka

If any one looking to specifying Java 11 as the version to be used in Spring (or Spring Boot) in a gradleproject, then use either sourceCompatibility = '11' or sourceCompatibility = '1.11' in your build.gradle file

如果有人希望在gradle项目中将Java 11 指定为要在 Spring(或 Spring Boot)中使用的版本,则在 build.gradle 文件中使用 sourceCompatibility = '11' 或 sourceCompatibility = '1.11'

plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
}

apply plugin: 'io.spring.dependency-management'

group = 'au.com.ranuka'
version = '0.0.1-SNAPSHOT'

sourceCompatibility = '11'
//or 
sourceCompatibility = '1.11'

repositories {
   mavenCentral()
}

dependencies {

}