Maven編譯Fatal?error?compiling:無效的目標(biāo)發(fā)行版:11問題及解決
Maven編譯Fatal error compiling:無效的目標(biāo)發(fā)行版:11
安裝了Java11后,用其編譯多個Springboot工程,老是失敗,后來發(fā)現(xiàn)所有工程指定了java版本為java8,于是乎卸載了java11,安裝了java8,再去編譯。
其中某個工程在之前用java11編譯的時候沒有任何問題, 但是換成java8后出現(xiàn)了以下錯誤:
具體信息:
F:\Digital marketing\workspace-scrm\workspace-scrm\wit-gateway-app>mvn clean install package -Dmaven.test.skip=true
[INFO] Scanning for projects...
Downloading from nexus: http://......:8051/repository/maven-public/net/trueland/tcloud/scrm/common-dependencies/3.1-SNAPSHOT/maven-metadata.xml
Downloaded from nexus: http://......:8051/repository/maven-public/net/trueland/tcloud/scrm/common-dependencies/3.1-SNAPSHOT/maven-metadata.xml (621 B at 3.8 kB/s)
[INFO]
[INFO] --------------< net.trueland.tcloud.scrm:tcloud-gateway >---------------
[INFO] Building tcloud-gateway 3.1.0
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from nexus: http://......:8051/repository/maven-public/net/trueland/commons/1.1-SNAPSHOT/maven-metadata.xml
Downloaded from nexus: http://......:8051/repository/maven-public/net/trueland/commons/1.1-SNAPSHOT/maven-metadata.xml (983 B at 16 kB/s)
Downloading from nexus: http://......:8051/repository/maven-public/net/trueland/tcloud/scrm/common-base/3.1-SNAPSHOT/maven-metadata.xml
Downloaded from nexus: http://......:8051/repository/maven-public/net/trueland/tcloud/scrm/common-base/3.1-SNAPSHOT/maven-metadata.xml (996 B at 15 kB/s)
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ tcloud-gateway ---
[INFO] Deleting F:\Digital marketing\wit-gateway-app\target
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ tcloud-gateway ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 0 resource
[INFO] Copying 6 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ tcloud-gateway ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 14 source files to F:\Digital marketing\workspace-scrm\wit-gateway-app\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.305 s
[INFO] Finished at: 2022-01-12T08:43:21+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project tcloud-gateway: Fatal error compiling: 無效的目標(biāo)發(fā)行版: 11 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
從網(wǎng)上查了查資料,需要指定Maven的java編譯器版本:
1.在Java 8和更早版本中指定Maven的Java編譯器
在Java 8 和更早版本開始,可以通過兩種方式在Maven POM文件中設(shè)置Java編譯器版本:
- 通過Maven Java編譯器屬性。
- 通過Maven Java編譯器插件。
下面將具體說明這兩種在Maven中設(shè)置Java編譯器版本的方法。
Maven Java編譯器屬性
最簡單的方法是通過Maven Java編譯器屬性。
在我們工程的pom.xml文件中進(jìn)行設(shè)置,這些屬性必須包含在POM文件的properties元素中。
<properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties>
下面是一個具體的例子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bruce</groupId> <artifactId>wit-example</artifactId> <version>0.1.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
Maven Java編譯器插件
第二種方式是通過插件:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
下面是一個具體的例子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bruce</groupId> <artifactId>wit-example</artifactId> <version>0.1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
2.在Java 9及以后的版本中指定Maven的Java編譯器
在Java 9和更高版本中,也要使用插件,但是要用release屬性來代替source和target屬性。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> </configuration> </plugin>
Note:Maven Java編譯器插件的版本已從3.6.1更改為3.8.0
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程通信中關(guān)于生產(chǎn)者與消費者案例分析
這篇文章主要介紹了Java線程通信中關(guān)于生產(chǎn)者與消費者案例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Springboot實現(xiàn)前后端分離excel下載
這篇文章主要介紹了Springboot實現(xiàn)前后端分離excel下載,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Mybatis原始執(zhí)行方式Executor代碼實例
這篇文章主要介紹了Mybatis原始執(zhí)行方式Executor代碼實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07JAVA 開發(fā)之用靜態(tài)方法返回類名的實例詳解
這篇文章主要介紹了JAVA 開發(fā)之用靜態(tài)方法返回類名的實例詳解的相關(guān)資料,這里主要說明使用異常來得到類名,希望能幫助到大家,需要的朋友可以參考下2017-08-08Java中的關(guān)鍵字synchronized 詳解
這篇文章主要介紹了Java中的關(guān)鍵字synchronized,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java使用jxl包寫Excel文件適合列寬實現(xiàn)
用jxl.jar包,讀寫過Excel文件。也沒有注意最適合列寬的問題,但是jxl.jar沒有提供最適合列寬的功能,上次用到寫了一下,可以基本實現(xiàn)最適合列寬。2013-11-11