Compile source code using Maven Compiler Plugin

To compile the source code in Maven, we will usually use the Maven Compiler Plugin. It allows us to configure the version of Java that we want to compile the source code. For example, if you write code using the syntax of Java 7 and you want to compile source code also Java 7 then you can configure it as follows:

You can also compile source code from one Java version to another. For example, if you are writing code in accordance with the standard of Java 7 but you want your code compiled in accordance with Java 6 standards, you can use Maven Compiler Plugin as follows:

If you used Java’s javac tool to compile the source code, then you will know the source and target tags in the configuration of this plugin correspond to the -source version and -target version options when you use javac to compile the source code.

You can also declare the following <properties> to configure the source and target of Java in the Maven project pom.xml file.

Then, you do not need to declare the source and target in the <configuration> section of Maven Compiler Plugin anymore.

Before Java 9, we used 1.x syntax with x as the Java version to declare the Java version in the Maven Compiler Plugin. From Java 9 onwards, you don’t need to do that anymore, just declare the exact Java version you want to use.

Now you can declare as follows:

The <properties> section can be declared as follows:

From Java 9 onwards, the javac tool also supports one more option, -release version to specify the Java version that you want to build a Java project. For Maven, you can declare as follows:

Add Comment