Maven and Eclipse have always had a rocky relationship, and a common pain point between the two is how to force Maven JDK 1.8 support in new Eclipse projects. Without jumping through a few configuration hoops, the antiquated Java 1.5 version persistently remains the default and to fix this you must add a reference to the Maven compiler plugin in the POM.
The Eclipse IDE became popular before the Maven revolution really took hold, so the IDE’s support for the build tool always felt like an afterthought. Even with recent releases like Eclipse Photon, tasks such as importing Maven projects or creating anything more than a basic Maven project is less than graceful.
Maven JDK 1.8 use in Eclipse
A reminder of this rocky relationship is the fact that Eclipse and Maven have a habit of forcing Java 5 compliance on new applications, even if JDK 1.8 is the only JVM installed on the development machine. The Java 1.5 compliance issue means Lambda expressions, stream access or newer language features besides generics won’t compile.
Some developers try and change the Java compiler setting within Eclipse from Java 1.5 to Java 1.8, but that only works temporarily. As soon as a new Maven build takes place, the JDK version reverts back to 1.5. The only way to make this a permanent change is to edit the Maven compiler plugin POM entry and force Eclipse and Maven to use Java 1.8.
Of course, there is a fairly simple answer to this problem. That’s why everyone loves Maven. The build tool always has a simple resolution on offer. To force Eclipse and the Maven compiler plugin to have JDK 1.8 compliance on your new projects, simply add the following to your POM file:
<!-- Force Eclipse and Maven to use a Java 8 JDK compiler--> <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties>
Maven Java 1.8 plugin support
An alternate, albeit slightly more verbose approach to tell Eclipse and Maven to use Java 8 or newer compilers is to configure the Apache Maven plugin:
<!-- Build plugin to force Maven JDK 1.8 compliance --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
Maven compiler plugin and Java 11
Note that since the 3.8.0 version of the Maven compiler plugin, you can actually specify the release version of Java rather than source and target:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> </configuration> </plugin>
Eclipse Java 1.8 compiler setting
Once the maven-compiler-plugin change is made to the POM, you can open up Eclipse’s Java compiler properties page and notice that JDK compliance has changed from JDK 1.5 to 1.8.
It’s a tedious problem but it’s also one that is easily fixed by a quick edit to the Maven compiler plugin. Furthermore, you only need to edit the POM file once on a project to force Eclipse and Maven to use Java 8 or Java 11. It’s really not all that big an inconvenience to overcome.