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

新手上路,请多包涵

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

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

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

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

例如

<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

我尝试了这两个,我的网络服务器似乎正确启动并正确编译(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 migration to JDK 11 and Spring 4.3.x with OpenJDK 11 ,但它们并没有真正阐明任何问题。

原文由 Acapulco 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.2k
2 个回答

简短回答:

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

  • Java 8:1.8 或 8
  • Java 9:9
  • Java 10:10
  • Java 11:11
  • Java 12:12
  • …..
  • …..
  • Java 17:17
  • Java 18:18
  • 爪哇 19:19

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

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

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

其实最后还是用 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值为 <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项目都会扩展父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.由于用户属性的行为,将这两个属性设置为 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 )。然后,从 javac 文档中,我们可以看到这两个参数允许具有以下值:

  • 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 中引入的功能的代码。

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

原文由 Ken Chan 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果有人希望在 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 {

}

原文由 Ranuka 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题