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

标签 java spring maven spring-boot pom.xml

将 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(尽管看起来不太可能),并且在使用 时我看到它被指定为 11 maven-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。

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

我尝试了两者,我的网络服务器似乎正确启动并正确编译(IntelliJ 显示信息:javac 11.0.2 用于编译 java 源),但是我并不完全相信更改java.version 值可以做任何事情,因为我可以将其设置为例如100 一切正常。

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

最佳答案

简短回答:

正确的方法是使用 <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
  • Java 19:19

所以对于 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.

其实最后还是用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项目都会扩展父pom spring-boot-starter-parent其中defines <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>

来自 maven-compiler-plugin docs , maven.compiler.sourcemaven.compiler.targetuser property对于<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>

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

  • 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.version>应设置为11适用于 Java 11。

关于java - 如何在Spring/Spring Boot pom.xml中指定Java版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467287/

相关文章:

mysql - Spring 启动: MySQL Duplicate key error when trying to map customers to items

java - Hibernate SessionFactory 如何使用 LocalSessionFactoryBean Autowiring ?

java - 无法将目标文件夹添加到 .gitignore

java - 保存用户从 Maven Clean 上传的文件

java - Spring Boot 连接到 AWS RDS MySQL - SSLHandshakeException : Received fatal alert: unknown_ca

java - Clojure - java 互操作 - 如何传递 float 组>

java - 在 Windows 上用 Java 并发写入文件

java - JSP 在我的 WEB-INF/lib 和构建路径中找不到 JDBC 驱动程序

java - 我应该在 Maven 的 Web (WAR) 项目中的什么地方添加 Java 文件?

java - 尽管在 pom 上有 commons-httpclient 和 httpcomponents 依赖,但在运行有效的 jar(使用依赖项编译)时出现 NoClassDefFoundError