本文介紹了如何在Spring/Spring Boot pom.xml中指定Java版本?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
將Java 11指定為在Spring(或Spring Boot)pom.xml文件中使用的版本的正確方式是什么?
即,我需要在pom的java.version
屬性中輸入什么值?
對(duì)于Java 8,我們使用1.8
,就像the documentation shows一樣,對(duì)于Java 9,它是1.9
。
我不確定Java 11是否會(huì)是1.11
(盡管它似乎不太可能),我看到它在使用maven-compiler-plugin
時(shí)只是指定為11
,但我沒(méi)有使用編譯器插件。
例如
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
我到處搜索,但都找不到答案,因?yàn)槲易x過(guò)的幾乎所有文檔以及與Java版本相關(guān)的博客文章都只顯示版本8或9。
那么我應(yīng)該使用什么?1.11
還是只使用11
?
我兩個(gè)都試過(guò)了,我的Web服務(wù)器似乎正確啟動(dòng)和正確編譯(IntelliJ顯示Information:javac 11.0.2 was used to compile java sources
),但我不完全相信更改java.version
值有什么作用,因?yàn)槲铱梢詫⑵湓O(shè)置為例如100
,一切正常。
我能找到的唯一相關(guān)問(wèn)題是:Minimum Spring version compatible with Java 11、Spring Boot fails due to a Hibernate error after migrating to JDK 11和Spring 4.3.x with OpenJDK 11,但它們并沒(méi)有真正揭示任何問(wèn)題。
推薦答案
簡(jiǎn)短回答:
正確的方法是對(duì)不同的Java版本使用<java.version>
中的下列值:
Java 8:1.8或8
Java 9:9
Java 10:10
Java 11:11
Java 12:12
…..
…..
Java 16:16
Java 17:17
因此,對(duì)于Java 11,應(yīng)該是:
<properties>
<java.version>11</java.version>
</properties>
然而,我不確定Java 11是否會(huì)是1.11&q;(似乎不太可能),以及
我看到它在使用maven-編譯器-plugin時(shí)被指定為11&q;
但是,我沒(méi)有使用編譯器插件。
實(shí)際上,它最后仍然使用maven-compiler-plugin
進(jìn)行編譯。SpringBoot只配置一個(gè)<java.version>
屬性,這樣通過(guò)更改此值,您就隱式地將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>
詳細(xì)答案:
您似乎想要詳細(xì)信息來(lái)說(shuō)服您。
因?yàn)槊總€(gè)Spring Boot項(xiàng)目都會(huì)對(duì)父POMspring-boot-starter-parent
defines<java.version>
進(jìn)行如下擴(kuò)展:
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
docs、maven.compiler.source
和maven.compiler.target
是<source>
和<target>
配置參數(shù)的user property。由于用戶屬性的行為,將這兩個(gè)屬性設(shè)置為11
意味著設(shè)置以下內(nèi)容:
<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>
是Java編譯器(javac
)的-source
和-target
參數(shù)。然后,從javac文檔中,我們可以看到這兩個(gè)參數(shù)被允許具有以下值:
1.6:Java SE 6中沒(méi)有引入任何語(yǔ)言更改。但是,源文件中的編碼錯(cuò)誤現(xiàn)在報(bào)告為錯(cuò)誤,而不是
警告與早期版本的Java Platform標(biāo)準(zhǔn)版相同。
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中引入的功能的代碼。
因此,對(duì)于Java 11,<java.version>
應(yīng)設(shè)置為11
。
這篇關(guān)于如何在Spring/Spring Boot pom.xml中指定Java版本?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,