dovexz12323
dovexz12323

Reputation: 277

Fatal error compiling: error: invalid target release: 17

I am trying to build a project via Jenkins, but I keep on getting this error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project my-project: Fatal error compiling: error: invalid target release: 17 -> [Help 1]

I have tried assigning source and target versions in pom.xml, wouldn't work.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>${maven-compiler-plugin.version}</version>
      <configuration>
         <target>17</target>
         <source>17</source>
      </configuration>
</plugin>

mvn --version output:

Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2022.3.3\plugins\maven\lib\maven3
Java version: 17.0.8, vendor: Eclipse Adoptium, runtime: C:\Program Files\Eclipse Adoptium\jdk-17.0.8.7-hotspot
Default locale: en_US, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

As a matter of fact, if I specify the source and target of versions as 1.8, it would then build correctly, but the project is using Java 17.

   <java.version>17</java.version>

What other ways of fixing I could try to make it work?

Upvotes: 1

Views: 14694

Answers (2)

peterh
peterh

Reputation: 19295

It may be due to Maven Plugin forking a new JVM

You might be using a Maven Plugin which forks (creates a new JVM for some sub-execution). This is not uncommon among Maven Plugins. For example, the Maven Release Plugin does this.

When this forking happens it will be using the Java which is on the PATH which gets executed, irrespective of the Java which was used to launch your Maven session.

In conclusion, in this scenario, you can check the output from mvn --version all you like; it will not give you relevant information.

Note that in this day and age it would probably be most sensible for such plugins to fork using the the JVM which the parent session is using. This would avoid the problem. Alas, such option rarely exists, at least not in the Maven Release Plugin.

(note: To make matters worse, the problem is not isolated to the Java version: The Maven Release Plugin executes the mvn command in the forked process so the same confusion applies to the Maven version)

Upvotes: 1

Parth Pancholi
Parth Pancholi

Reputation: 85

I think the configurations for java 8 with maven-compiler-plugin and java 17 with maven-compiler-plugin is different ways to define it.

For java 8 and lower version it's as below,

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>

For java 9+ version the configurations are as below,

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
      <release>8</release>
    </configuration>
  </plugin>

References:

Upvotes: 5

Related Questions