maven设置jdk有两种方法,setting.xml或pom.xml里面设置,pom里面设置也有两种。
setting.xml里面设置的话,是设置成全局变量,在setting.xml文件中的profiles元素下添加如下profile元素
<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>
然后在各自项目的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>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
还有
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
以上都可以指定jdk版本
但是还有一个问题就是,maven指定了jdk之后,他只能使用他指定的javase1.8,如果这时候jdk设置不对,就会出现这样的情况
使用的lib是jre版本,如果这时候使用maven进行打包之类的操作,就会提示
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
打开Window - Preferences - java - Installed JREs
发现里面使用的是jre,而不是jdk
这时候需要添加上jdk,并且切换成jdk
但是编译的时候依旧报错。
回头再看项目的java build path
发现上面的javaSE-1.8(jre1.8.0_131),里面使用的是jre而不是jdk
而且打开选项也没有jdk的选择。
这是因为一开始使用的是jre而不是jdk,导致默认的javaSE是使用的jre。
这里只需要删除 Installed JREs中的jre那条JREs就行了