How to use a specific version of Java in Maven
This shot discusses different ways to set the Java version in Maven projects.
What is Maven?
Maven is a tool used to build and package Java applications.
Method 1: Change the JAVA_HOME path
Maven uses the JAVA_HOME environment variable to find which Java version it is supposed to run. If a different version of Java is needed, set the JAVA_HOME to the path of the specific version of JDK.
For example, if Java 8 is needed:
JAVA_HOME=/usr/local/java8
Method 2: Maven Compiler plugin properties
The following properties can be configured to set the Java version in Maven.
maven.compiler.sourcemaven.compiler.target
In the pom.xml, add the above two properties.
If Java 8 is needed:
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Method 3: Configure the Maven Compiler plugin
Add the source and target while you configure the Maven compiler plugin in the pom.xml file.
If Java 8 is needed, refer to the following code.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>