Bitbucket Pipelines - Maven compile error "invalid target release"

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

The pipeline build may fail with the following Maven compile error:

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

Cause

The above error may occur due to a mismatch of Java versions used by Maven. The default Atlassian Docker image used in the pipelines comes with pre-installed Maven and Java. Maven may be using a different Java version to compile the project, resulting in the error.

Example: Let's run a pipeline build to find Maven version details and the Java version used by the same.

bitbucket-pipelines.yml

1 2 3 4 5 6 pipelines: default: - step: script: - java -version - mvn --version

Pipeline Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 + java -version java version "1.8.0_151" Java(TM) SE Runtime Environment (build 1.8.0_151-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode) + mvn --version Apache Maven 3.0.5 Maven home: /usr/share/maven Java version: 1.8.0_151, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-oracle/jre Default locale: en_US, platform encoding: ANSI_X3.4-1968 OS name: "linux", version: "5.10.101", arch: "amd64", family: "unix"

As you can see, Maven is using Java 1.8, and hence the error "Fatal error compiling: invalid target release: 17"

Solution

Upgrade the Java version to 17 or the latest version as defined in your pom.xml file in the pipelines, and update the JAVA_HOME environment variable to ensure that it points to the correct Java version. One can also utilize the official Maven Docker image within their pipelines, as it is equipped with the necessary latest Java version.

Example:

bitbucket-pipelines.yml

1 2 3 4 5 6 image: maven:3.9.0 pipelines: default: - step: script: - mvn --version

Pipeline Output:

1 2 3 4 5 6 + mvn --version Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584) Maven home: /usr/share/maven Java version: 17.0.6, vendor: Eclipse Adoptium, runtime: /opt/java/openjdk Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "5.10.101", arch: "amd64", family: "unix"
Updated on April 11, 2025

Still need help?

The Atlassian Community is here for you.