1.为什么要指定maven编译版本
有时候我们发现写代码的时候没问题,使用maven项目打包的时候失败,原因是编译失败。如maven项目的jdk版本是jdk1.8,并且使用了1.8的新特性,但是maven编译版本是jdk1.7,编译无法通过。
2. 编译版本设置
maven是个项目管理工具,如果没有设置jdk版本编译的话,它就会用maven-compiler-plugin默认的jdk版本进行处理,这样就容易出现版本不匹配的问题。在构建maven项目的时候,需要配置maven-compiler-plugin插件的版本。maven3 compiler插件默认值支持Java1.5,需要进行设置,有两种方法
- 修改pom.xml 文件添加下面代码,修改版本号即可
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
- 修改maven安装目录conf/settings.xml文件, 适用全局,在profiles节点新增
<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>