1 背景
在做项目时,需要使用到maven进行打包,当我使用命令:
mvn clean package -Dmaven.test.skip=true
进行打包操作时,发现报以下错误:
2 解决方案
2.1 查看设置,确认是jdk1.8
1、Project Settings -> Project/ Modules/ SDKs:
注意:所有的都改为jdk1.8,语言级别也改为8。
2、settings -> Build,... -> Java Compiler -> pre-module ...:
注意:也都改为8
3、执行mvn clean package -Dmaven.test.skip=true:
依然报错,接下来...
2.2 查看maven的settings.xml
0、settings.xml在哪里?
settings -> Build,... -> Build Tools -> Maven:
1、settings.xml修改前,jdk为9.0,注意右上角profiles-jdk9.0:
2、settings.xml修改为8.0后,注意右上角profiles-jdk8.0:
settings.xml:
<profile>
<id>jdk-9.0</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>9.0</jdk>
</activation>
<properties>
<maven.compiler.source>9.0</maven.compiler.source>
<maven.compiler.target>9.0</maven.compiler.target>
<maven.compiler.compilerVersion>9.0</maven.compiler.compilerVersion>
</properties>
</profile>
此时执行mvn clean package -Dmaven.test.skip=true,仍然报错,只是错误信息不同:变成了“无效的发行版本8.0”,于是想到为什么不是“无效的发行版本1.8”?所以想起修改了settings.xml的jdk为8.0,所以应该是这里jdk版本的问题。
3、settings.xml修改为1.8后,注意右上角profiles-jdk1.8:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
此时再执行 mvn clean package -Dmaven.test.skip=true,大功告成:
成功:
生成jar包:
3 总结
1、setting->jdk版本;
2、maven的settings.xml。