Home Maven How to set Java version in Maven?

How to set Java version in Maven?

In this quick guide, we discuss how to set specific java version in Maven configuration. There are several ways to tell Maven to which Java compiler version to use. Let’s discuss most commonly used options.

1. Default compiler version

With Maven, if you do not specify any compiler version explicitly, Maven uses Java 1.5 compiler version.

Before move on ahead, let’s understand how to compile java code in specific compiler version using JDK.

2. Understanding Compiling java code to specific version

2.1. javac tool provides -source, -target and -bootclasspath options to compile Java code to specific version.

 $ javac -help

 -source <release>      Provide source compatibility with specified release
 -target <release>      Generate class files for specific VM version
 -bootclasspath <path>  Override location of bootstrap class files

-source option verifies whether source code compatible with specified java release version.

-target option compiles the source code to specified version.

Example: Suppose you have installed Java 8 in your machine, but you want to compile source code in 1.6.

$ javac MyTest.java -source 1.6 -target 1.6 -bootclasspath "%JAVA_HOME/jre/lib/rt.jar"

The above command successfully compiles source code against Java 1.6 version if the source code compatible with 1.6. If -bootclasspath not used, the source code do not compile correctly to the target version.

2.2. The above process works well until Java 8 version. In java 9 version, introduced a new option -release to support cross-compiler versions. -release option replaces three of these -source-target and -bootclasspath

Java 9 and above versions, can be used single flag -release to compile source code to specific version.

Example: For example JDK 11 is installed in your machine, to compile against Java 8.

$ javac MyTest.java --release 8

Now let’s see the maven configuration options to set java version.

3. Java 8 and before versions

If you are using Higher version than Java 1.5, the version must be specified either using Maven project properties or by using Maven Compiler plugin.

3.1. Set version using properties

We can set the Java compiler version through the maven.compiler.source and maven.compiler.target properties. The source and target only valid 1.3, 1.4, 1.5 or 5, 1.6 or 6, 1.7 or 7, 1.8 or 8.

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

3.2. Set version using plugin

We also can set the Java version using the source and target configuration options of Compiler Plugin. If we don’t set the source and target values, plugin takes 1.6 as the default version.

			<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>

4. Java 9 and above versions

Java 9 and above versions, source code must be compatible with Java 1.5 or above. Means javac -source does not support lower than 1.5 version. Valid Java versions to specify is 1.661.771.8 or 8, 9, 10 , 11 and 13.

As we discussed earlier, In java 9 version, a new option -release to support cross-compiler versions it replaces three of these -source-target and -bootclasspath.

4.1. Set version using properties

We can set the Java compiler version through the maven.compiler.release. Make sure your plugin version must be 3.6 or above.

<properties>
    <maven.compiler.release>9</maven.compiler.release>
</properties>

4.2. Set version using plugin

We can set the Java version by configuring the release option of the Plugin.

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
				       <release>8</release>
				</configuration>
			</plugin>

4.3. Additional compiler arguments

Sometimes for java newer versions, for example preview features, we may need to pass additional arguments to compiler to make it work. We can pass additional arguments to the compiler using compilerArgs option.

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
				<configuration>
				       <release>15</release>
                                       <compilerArgs>--enable-preview</compilerArgs>
				</configuration>
			</plugin>

5. Conclusion

In this quick guide, we learned how to set specific java version in Maven configuration.

You also might interested in following Maven guides:

  1. Install Maven on Windows
  2. Install Maven on Mac
  3. Install Maven on Ubuntu
  4. Import maven project into Eclipse
  5. Maven skip tests
  6. Maven run tests
  7. Maven run java main class

6. Reference

  1. Maven Document
  2. javac -bootclasspath

LEAVE A REPLY

Please enter your comment!
Please enter your name here