将 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 11 , Spring 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 许可协议
简短回答:
正确的方法是在
<java.version>
中为不同的 Java 版本使用以下值:所以对于 Java 11 ,它应该是:
其实最后还是用
maven-compiler-plugin
来编译。 Springboot just configures a<java.version>
property such that by changing this value , you are implicitly changingmaven-compiler-plugin
’s<source/>
and<target/>
to the same值为<java.version>
中指定的值:详细答案:
看起来你想要细节来说服你。
这是因为每个spring boot项目都会扩展父pom
spring-boot-starter-parent
定义<java.version>
如下:From the maven-compiler-plugin docs ,
maven.compiler.source
andmaven.compiler.target
are the user property for the<source>
and<target>
config parameters.由于用户属性的行为,将这两个属性设置为11
意味着设置如下:From the
maven-compiler-plugin
docs again,<source>
and<target>
are the-source
and-target
argument for the Java compiler (javac
)。然后,从 javac 文档中,我们可以看到这两个参数允许具有以下值:因此,对于 Java 11,
<java.version>
应该设置为11
。