In General, the cause of the title problem is the incompatibility between the version of JDK used in the project and properties responsible for compatibility of the source and target code. These problems often occur in tandem as it is shown on the following listing:
Compilation failure: Compilation failure: [ERROR] Source option 5 is no longer supported. Use 6 or later. [ERROR] Target option 1.5 is no longer supported. Use 1.6 or later. [ERROR] -> [Help 1]
The tables below contain the supported values of source and target compatibility depending on the JDK version and the default source/target setting depending on the maven-compiler-plugin version.
JDK Version | source compatibility version | target compatibility |
---|---|---|
before JDK9 | 5+ | 6+ |
JDK9+ | 1.6+ | 1.6+ |
Default version of maven-compiler-plugin | Default version of source compatiblity | Default version of target compatiblity |
---|---|---|
before 3.8.0 | 5 | 1.5 |
3.8.0+ | 6 | 1.6 |
I marked the most common cause of the title problem in red and yellow-red. I mean – using the new version of JDK (eg JDK9), which no longer supports Java 1.5, and the old version of maven-compiler-plugin, which defaults to Java 5 for source / target compatibility.
We already know the cause of the problem, so we can move on to discussing the solutions, because they differ slightly depending on the circumstances.
Situation 1: New project uses JDK 9+
This is the most common situation in which we can get the title errors. It will probably result from the fact that the default projects created do not set source / target compatibility, and in addition it uses an older version of maven-compiler-plugin (older than 3.8.0).
Solution 1: Use newer version of maven-compiler-plugin
We can take advantage of the fact that maven-compiler-plugin starting from 3.8.0 defaults source/target to Java 6 and just explicitly points to the newer version of the plugin. To do this, edit the pom.xml file by adding or updating the section below.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bettercoding.hex</groupId> <artifactId>HexagonalArchitectureExample</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>CreditWorkflow</module> </modules> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> </plugins> </build> </project>
If your application does not need to be compatible with such an old version of Java as 1.6, you can immediately set this value to e.g. 11, to be able to enjoy all the benefits of Java 11.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bettercoding.hex</groupId> <artifactId>HexagonalArchitectureExample</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>CreditWorkflow</module> </modules> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>11</release> </configuration> </plugin> </plugins> </build> </project>
Situation 2: Full control over maven-compiler-plugin, source and target compatibility
If you want to use a specific version of maven-compiler-plugin or you just want full control over source and target compatibility, you can use the solution below.
Solution 2. Set the proper maven properties
In that case, you can add the following section to pom.xml, which will set the value of source / target compatibility to the value you want – in this case, Java 11 compatibility.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bettercoding.hex</groupId> <artifactId>HexagonalArchitectureExample</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>CreditWorkflow</module> </modules> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> </project>
At the end… May I ask you for something?
If I helped you solve your problem, please share this post. Thanks to this, I will have the opportunity to reach a wider group of readers. Thank You
Thanks Lukasz Ciesla, the given solution resolved the issue.
Regards
Devaraj