maven中jdk的配置分為全局配置和局部配置兩種方式谚咬。
一、全局配置
全局配置是指在${MAVEN_HOME}\conf\settings.xml中進行配置,注意${MAVEN_HOME}指的是maven的安裝目錄茴肥。例如还最,要配置jdk1.8,打開settings.xml這個文件墓阀,然后在<profiles> </profiles>之間添加如下代碼。
<profile>
<id>jdk18</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>
全局配置的好處就是省事拓轻、方便斯撮。一次配置以后,再使用maven構(gòu)建項目扶叉,項目編譯時勿锅,默認使用jdk1.8進行編譯帕膜。
其實我們開發(fā)中一般有IDE自帶的Maven插件,那么這種就用不上了溢十,這時垮刹,我們要到用戶目錄下的setting.xml里面去配置一下,讓我們每次利用插件生成的maven項目都是指定的jdk版本张弛,默認的好像是jdk1.5荒典,我這里改為jdk1.8的。我這里是在C:\Users\NSK.m2目錄下的setting.xml.
打開這個配置文件吞鸭,然后在<profiles> </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>
這樣一來寺董,你每次在IDE下創(chuàng)建的Maven項目,都是jdk1.8版本的刻剥。這個更常用螃征。
二、 局部配置
局部配置就是只針對具體某個項目進行配置的,就是項目已經(jīng)創(chuàng)建好了透敌,但是jdk的版本跟實際不一樣盯滚。具體就是,在項目的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>