コンパイルでのJavaバージョンを指定するには
提供: tknotebook
メインページ>コンピュータの部屋#Java>Maven Tips
Maven でJavaのコンパイルを行うと、Javaコンパイラのバージョンは既定で 1.5 になります。
これはもうかなり古いコンパイラバージョンなので不適切であることが多いでしょう。
maven-compiler-plugin の設定を変更するのが正攻法で、pom.xml の buildタグの中に(なければbuildタグを加え) 以下の記述を加えます。
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
以上が正攻法ですが、これよりももっと簡単な方法があります。pom.xml の末尾(projectタグの内側の一番最後)に
<properties>
<java.version>1.8</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
</properties>
を加える方法です。maven.compiler.target/maven.compiler.source プロパティは、maven-compiler-plugin が参照しているコンパイラバージョンの既定値で、このプロパティが存在していない場合は、コンパイラバージョンは 1.5 になります。