Explain Codes LogoExplain Codes Logo

How do you specify the Java compiler version in a pom.xml file?

java
maven
compiler-plugin
java-compiler
Anton ShumikhinbyAnton Shumikhin·Sep 22, 2024
TLDR

You can specify the Java compiler version in your Maven pom.xml file by setting the source and target values in the maven-compiler-plugin.

Just pop this little snippet between your <plugins> tags and voila, instant Java compiler version action:

<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <!-- latest version as of writing this --> <configuration> <source>1.8</source> <!-- Java version for source code aka "the recipe" --> <target>1.8</target> <!-- Java version for compiled classes aka "the pie" --> </configuration> </plugin>

And don't forget to specify the maven.compiler.source and maven.compiler.target properties in the <properties> section, like so:

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

Now your Maven project will be baking with Java 1.8. Bon appétit!

Maven compiler plugin deep dive

Juggling different version balls

The maven-compiler-plugin has different default source and target settings, depending on its version. For instance, version 3.8.0 defaults to 1.6. So mentioning the plugin version explicitly saves unexpected surprises by giving you more control over your build:

<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <!-- Version control: Because surprises are fun only at parties --> </plugin>

Using the magical release parameter

Since JDK 9, the release parameter can compile source code and classes using a single command. Now you can easily adapt to newer versions like Java 11 or 17:

<configuration> <release>11</release> <!-- knockout punch of source and target versions --> </configuration>

Cross-compilation made easy

If you are working on cross-compilation, adjust source, target, and JVM options accordingly in maven-compiler-plugin. Always perform thorough testing to prevent a sudden case of "it worked on my machine":

<configuration> <source>1.8</source> <target>1.8</target> <compilerArgs> <arg>-Xlint:unchecked</arg> <!-- to escape from lint monsters --> </compilerArgs> </configuration>

Sifting through the POM coffee beans

Just like checking your beans before grinding them for coffee, run the Effective POM command after setting your version to make sure changes are properly stashed:

mvn help:effective-pom

Matching your caffeine level

Remember to sync up your IDE with the pom.xml file to reflect the compiler version changes. Look for an "Update Project" or "Re-import" option.

Detailed tweaks and troubleshooting

Attention to plugin configuration

Specify your compiler configuration details in the pom.xml within the build>plugins>plugin>configuration block. Extra clarity in these areas keeps your build ticking as smoothly as a Swiss watch:

<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- All the good stuff for Java Compiler version and options goes here --> </configuration> </plugin> </plugins>

Determining the Maven version

For building consistency, mention the version of Maven to avoid any awkward "oops" moments between the developers' environment and continuous integration (CI) servers:

<prerequisites> <maven>3.6.3</maven> </prerequisites>

Forking showdown

Sometimes particularly with legacy projects or finicky Java versions, forking a separate Java compiler process is essential:

<configuration> <fork>true</fork> <!-- Multiple tasks, multiple forks. Jakob's fork principle --> </configuration>

Resolving generics and annotations’ puzzles

Each new Java version brings improved type inference and annotation handling. If you're stuck on issues around these, ensure to use a JDK with updated features.

References