简体   繁体   English

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

[英]How to specify Java version in 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?将 Java 11 指定为要在 Spring(或 Spring Boot)pom.xml 文件中使用的版本的正确方法是什么?

Ie, what value do I need to put in the java.version property of the pom?即,我需要在 pom 的java.version属性中输入什么值?

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 11 when using maven-compiler-plugin , however I'm not using the compiler plugin.我不确定 Java 11 是否为1.11 (尽管这似乎不太可能),并且我在使用maven-compiler-plugin时看到它仅指定为11 ,但是我没有使用编译器插件。

eg例如

<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.11 or 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.version value is doing anything, because I can set it to eg 100 and everything works fine.我尝试了这两个,我的 web 服务器似乎正确启动并正确编译(IntelliJ 显示Information:javac 11.0.2 was used to compile java sources )但是我不完全相信更改java.version值正在做任何事情,因为我可以将其设置为例如100 ,一切正常。

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.我能找到的唯一相关问题是: Minimum Spring version compatible with Java 11Spring Boot fails due to a Hibernate error after migrating to JDK 11 and Spring 4.3.x with OpenJDK 11 ,但他们真的不亮 11

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 8:1.8 或 8
  • Java 9 : 9爪哇 9 : 9
  • Java 10 : 10爪哇 10:10
  • Java 11 : 11爪哇 11 : 11
  • Java 12 : 12爪哇 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-plugin to compile.其实最后还是用maven-compiler-plugin来编译。 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> : Springboot 只是配置了一个<java.version>属性,这样通过更改这个值,你就隐式地将maven-compiler-plugin<source/><target/>更改为与<java.version>指定的值相同的值<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-parent which 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.source and maven.compiler.target are the user property for the <source> and <target> config parameters.从 maven-compiler-plugin docsmaven.compiler.sourcemaven.compiler.target<source><target>配置参数的用户属性 Due to the behaviour of the user property, setting these two properties to 11 means to set the following :由于用户属性的行为,将这两个属性设置为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-plugin docs again, <source> and <target> are the -source and -target argument for the Java compiler ( javac ).再次从maven-compiler-plugin 文档中<source><target>是 Java 编译器 ( javac ) 的-source-target参数。 Then, from javac docs, we can see that these two arguments are allowed to have the following values:然后,从javac docs 中,我们可以看到这两个参数允许具有以下值:

  • 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. 1.6 :Java SE 6 中没有引入任何语言更改。但是,源文件中的编码错误现在报告为错误,而不是像 Java Platform, Standard Edition 的早期版本那样报告为警告。
  • 6 : Synonym for 1.6. 6 : 1.6 的同义词。
  • 1.7 : The compiler accepts code with features introduced in Java SE 7. 1.7 :编译器接受具有 Java SE 7 中引入的特性的代码。
  • 7 : Synonym for 1.7. 7:1.7 的同义词。
  • 1.8 : The compiler accepts code with features introduced in Java SE 8. 1.8 :编译器接受具有 Java SE 8 中引入的特性的代码。
  • 8 : Synonym for 1.8. 8:1.8 的同义词。
  • 9 : The compiler accepts code with features introduced in Java SE 9. 9 :编译器接受具有 Java SE 9 中引入的特性的代码。
  • 10 : The compiler accepts code with features introduced in Java SE 10. 10:编译器接受具有 Java SE 10 中引入的特性的代码。
  • 11 : The compiler accepts code with features introduced in Java SE 11. 11 :编译器接受具有 Java SE 11 中引入的特性的代码。
  • 12 : The compiler accepts code with features introduced in Java SE 12. 12 :编译器接受具有 Java SE 12 中引入的特性的代码。

Hence, <java.version> should be set to 11 for Java 11.因此, <java.version>应设置为11的Java 11。

If any one looking to specifying Java 11 as the version to be used in Spring (or Spring Boot) in a gradle project, 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 {

}

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.您使用<release>11</release>指定 JDK 版本的方式是正确的。 It was introduced with maven-compiler-plugin 3.6 and is equivalent as the old way, as below:它是在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>

This post can be helpful on using maven-compiler-plugin with JDK 11. 这篇文章对在 JDK 11 中使用maven-compiler-plugin很有帮助。

You should not need to mention the maven-compiler-plugin in your POM file, if you only want to specify the compiler version.如果您只想指定编译器版本,则不需要在 POM 文件中提及 maven-compiler-plugin。 Instead, add these properties to your pom file相反,将这些属性添加到您的 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.编辑:糟糕,从 maven-compiler-plugin 的 3.6 版本开始,版本号属性似乎发生了变化。 The example above is fine for java 8 and lower.上面的示例适用于 java 8 及更低版本。 For java 9, add this property:对于 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 版本 - 属性和编译器插件之间的差异

Gradle API provides an enum for JavaVersion . Gradle API 为JavaVersion提供enum We can user these constant to define sourceCompatibility .我们可以使用这些常量来定义sourceCompatibility

eg.例如。 sourceCompatibility = JavaVersion.VERSION_11

Below are the few constants下面是几个常量

VERSION_1_8 - Java8
VERSION_1_9 - Java9
VERSION_1_10- Java10
VERSION_11 - Java11
VERSION_12 - Java12
VERSION_13 - Java13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM