'Java 17 and maven-archetype-quickstart gives error: Source Option 5 is no longer supported

When using the maven command to create a simple Maven project (with the latest versions of maven and java), I get the error:

[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

So, what change is needed to fix this. Do I change the maven command or edit the pom.xml file afterwards?

To reproduce the error

First verify what versions of Java and Maven you are running. At the time of this question, I'm using the latest versions of Maven and Java. Maven: 3.8.4 and Java 17.0.2
Below are details

mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)

Create the maven hello world-like program

I used this command to create the Java maven sample program.

mvn archetype:generate -DgroupId=com.example -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

I found the following link which explains this in more detail (if needed). https://facingissuesonit.com/2017/06/06/how-to-create-maven-java-console-project/.

Build the code

Next, build the code and observe the error.

cd helloworld
mvn clean install
[ERROR] COMPILATION ERROR :
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

Notice the ERRORs.

But, if I switch back to Java 8 (JDK 1.8) and run the maven build command, then the code compiles successfully. Below is the successful compile and the maven version and JDK version used that result in a successful compilation.

mvn clean install
[INFO] BUILD SUCCESS

mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: D:\p\jdk1.8.0_271\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

My question is, how do I fix this? and Why does this happen?



Solution 1:[1]

The problem occurs because the maven plugin (maven-compiler-plugin:3.1) passes the arguments -source 1.5 -target 1.5 to the JDK 17 java compiler.

The JDK 17 compiler doesn't support generating code for the Java 1.5 runtime so the error is produced as shown:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile 
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.

You can see this if you use the -X debug flag when running the maven command as shown below:

mvn -X clean install
. . . lots of output omitted . . .
[DEBUG] Command line options:
[DEBUG] -target 1.5 -source 1.5 -d target\classes -classpath target\classes; -sourcepath src\main\java; -g -nowarn
. . .

The fix is to modify the pom.xml file so that the maven-compiler-plugin will pass options like -source 1.8 -target 1.8 (or some higher number) so that the JDK 17 can produce code that is targeted for a version of the JDK that the JDK 17 compiler supports.

For example, to use JDK 1.8, you would add the following to your pom.xml file.

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

The question and answer https://stackoverflow.com/a/60344341/3281336 also might add some more information if you are interested.

NOTE: The 3.1 version of the maven-compiler-plugin is very old (released Apr 2013) based on https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin so back then, Java version 1.5 was a viable choice.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PatS