Codementor Events

Cookbook in Java development - JDK specification in Maven

Published Mar 07, 2019

I am writing this blog to share and log the solutions that I have found when I am using Maven to orginize my project. This blog may be updated frequently

How to specify the JDK version?

There are 3 ways to specify the JDK version in maven

1) java.version
This method is provided by Spring Boot only. If you are not using Spring Boot find method 2) or 3)

<properties>
    <java.version>1.8</java.version>
</properties>

2) maven.compiler.source
This method is provided by the maven core compiler plugin, you can use it directly

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

3) maven-compiler-plugin
This method is provided by maven-compiler-plugin, however, it equals to the method 2) as described in the document

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.0</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
  </configuration>
</plugin>

Since Java 9

Since Java 9 you should use maven.compiler.release or <release> to specify the JDK version

Cross-Compilation

When the JAVA_HOME version is lower then the target version, you may need to specify the JDK executable location to tell the plugin how to locate the compiler

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <compilerVersion>1.8</compilerVersion>      
        <fork>true</fork>
        <executable>/the/location/of/javac</executable>                
    </configuration>
</plugin>

References

The above information can be found in this stackoverflow post. I am recording the answer and will update in this post if anything changed.

Discover and read more posts from JamesPoon
get started