How do you specify the Java compiler version in a pom.xml file?
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>(whatever version is current)</version>
<configuration>
<!-- or whatever version you use -->
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
See the config page for the maven compiler plugin:
http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html
Oh, and: don't use Java 1.3.x, current versions are Java 11 or 17.
maven-compiler-plugin it's already present in plugins hierarchy dependency in pom.xml. Check in Effective POM.
For short you can use properties like this:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I'm using Maven 3.2.5.
Generally you don't want to value only the source
version (javac -source 1.8
for example) but you want to value both the source
and the target
version (javac -source 1.8 -target 1.8
for example).
Note that from Java 9, you have a way to convey both information and in a more robust way for cross-compilation compatibility (javac -release 9
).
Maven that wraps the javac
command provides multiple ways to convey all these JVM standard options.
How to specify the JDK version?
Using maven-compiler-plugin
or maven.compiler.source
/maven.compiler.target
properties to specify the source
and the target
are equivalent.
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
and
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
are equivalent according to the Maven documentation of the compiler plugin
since the <source>
and the <target>
elements in the compiler configuration use the properties maven.compiler.source
and maven.compiler.target
if they are defined.
source
The
-source
argument for the Java compiler.
Default value is:1.6
.
User property is:maven.compiler.source
.
target
The
-target
argument for the Java compiler.
Default value is:1.6
.
User property is:maven.compiler.target
.
About the default values for source
and target
, note that
since the 3.8.0
of the maven compiler, the default values have changed from 1.5
to 1.6
.
<release>
tag — new way to specify Java version in maven-compiler-plugin
3.6
You can use the release
argument :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>9</release>
</configuration>
</plugin>
You could also declare just the user property maven.compiler.release
:
<properties>
<maven.compiler.release>9</maven.compiler.release>
</properties>
But at this time the last one will not be enough as the maven-compiler-plugin
default version you use doesn't rely on a recent enough version.
The Maven release
argument conveys release
to the Java compiler to access the JVM standard option newly added to Java 9, JEP 247: Compile for Older Platform Versions.
Compiles against the public, supported and documented API for a specific VM version.
This way provides a standard way to specify the same version for the source
, the target
and the bootstrap
JVM options.
Note that specifying the bootstrap
is a good practice for cross compilations and it will not hurt if you don't make cross compilations either.
Which is the best way to specify the JDK version?
Java 8 and below
Neither maven.compiler.source
/maven.compiler.target
properties or using the maven-compiler-plugin
is better.
It changes nothing in the facts since finally the two ways rely on the same properties and the same mechanism : the maven core compiler plugin.
Well, if you don't need to specify other properties or behavior than Java versions in the compiler plugin, using this way makes more sense as this is more concise:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Java 9 and later
The release
argument (third point) is a way to strongly consider if you want to use the same version for the source
and the target
.