安装了Java11后,用其编译多个Springboot工程,老是失败,后来发现所有工程指定了java版本为java8,于是乎卸载了java11,安装了java8,再去编译。
其中某个工程在之前用java11编译的时候没有任何问题, 但是换成java8后出现了以下错误:
具体信息:
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: 无效的目标发行版: 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
从网上查了查资料,需要指定Maven的java编译器版本:
1. 在Java 8和更早版本中指定Maven的Java编译器
在Java 8 和更早版本开始,可以通过两种方式在Maven POM文件中设置Java编译器版本:
- 通过Maven Java编译器属性。
- 通过Maven Java编译器插件。
下面将具体说明这两种在Maven中设置Java编译器版本的方法。
1. Maven Java编译器属性
最简单的方法是通过Maven Java编译器属性。
在我们工程的pom.xml文件中进行设置,这些属性必须包含在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>
2.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