Build, Compile Maven Project với JDK 1.8 (Java 8)
Mặc định, sau khi tạo Maven Project, nó sẽ được compile bởi bản JDK J2SE-1.5.
Để chỉ rõ phiên bản JDK mà bạn muốn dùng để Build, Complie Maven Project ta làm như sau:
Cách 1: Sử dụng Maven Properties
Ví dụ bạn muốn dùng Java 8 để build, compile thì ta thêm đoạn sau vào file pom.xml
<properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties>
Ví dụ bạn muốn dùng Java 7 để build, compile
<properties> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.source>1.7</maven.compiler.source> </properties>
Với những version khác, bạn làm tương tự.
Cách 2: Sử dụng Maven Complier
Ví dụ bạn muốn dùng Java 8 để build, compile thì ta thêm đoạn sau vào file pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Trường hợp muốn chỉ rõ bản JDK trên Local
Bạn có thể chỉ rõ bản jdk trên local để build bằng cách sau:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable><!-- path-to-javac --></executable>
<compilerVersion>1.3</compilerVersion>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Trong đó path-to-javac là path tới file javac dùng để thực thi.
Ví dụ: C:\Program Files\Java\jdk1.8.0_171\bin\javac
Okay, Done!
Download project maven ví dụ trên tại đây.
References:
https://maven.apache.org/…/set-compiler-source-and-target.html
https://maven.apache.org/…/compile-using-different-jdk.html

