從碼雲上拉下來一個項目運行的時候,報了錯誤:
我的jdk是用的1.8,我知道1.8是允許這種寫法的,但是為什么會報這個錯誤呢,我File -> Project Structure
看到依賴是用的1.8
但是,它明明告訴我,用的是1.5
點開Sources:
發現Language level 被指定為1.5 ,但是在我另外一台電腦上明明是可以的,是什么時候被重新指定為1.5 了呢。
研究發現,可能和Maven有關系。
我用的是 maven-compiler-plugin:3.1 (網絡原因,報紅,不過和這個沒關系)
參考官網的介紹:http://maven.apache.org/plugins-archives/maven-compiler-plugin-3.1/
Maven Compiler Plugin
The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using
javac
, you must configure the plugin optionforceJavacCompilerUse
.Also note that at present the default
source
setting is1.5
and the defaulttarget
setting is1.5
, independently of the JDK you run Maven with. If you want to change these defaults, you should setsource
andtarget
as described in Setting the -source and -target of the Java Compiler.Other compilers than
javac
can be used and work has already started on AspectJ, .NET, and C#.This Compiler Plugin corresponds to Maven 1.x's Java Plugin.
NOTE: To know more about the JDK javac, please see: http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html.
大意是,從3.0 開始, 默認源碼的設置級別是1.5,與運行Maven的jdk版本無關。
如果不特別指定,Language level 的級別會一直是1.5 ,可能會引起一些不必要的麻煩,所以我們一般會在項目的pom文件里指定源碼的jdk版本。
<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項目構建的一些知識點了解不夠深入。