Skip to content

Instantly share code, notes, and snippets.

@welmarr
Last active April 22, 2025 10:03
Show Gist options
  • Save welmarr/325f25618b98fb8a789becd46ffe0d9a to your computer and use it in GitHub Desktop.
Save welmarr/325f25618b98fb8a789becd46ffe0d9a to your computer and use it in GitHub Desktop.
In this article, I explore the process of handling multiple versions of Java with Maven. Specifically, I focus on resolving an error that occurs when running a new Spring Boot application (version 3.0.1) when attempting to run the "mvn clean install" command line on a system with Java 11.

Managing Multiple Java Versions with Maven 🀹 (without modifying JAVA_HOME almost)

Today, I try to run a new spring boot application (<version>3.0.1</version>) on my Windows 11 but, when I perform the command line mvn clean install (mvn -version [Apache Maven 3.8.6 | Java version: 11.0.15.1]), I have the following issue πŸ‘‡

Execution repackage of goal org.springframework.boot:spring-boot
-maven-plugin:3.0.1:repackage failed: Unable to load the mojo 'repackage' in the plugin 'org.springframework.boot:spring-boot-maven-plugin:3.0.1' due to an API incompatibility: org.codehaus.plexus.comp
onent.repository.exception.ComponentLookupException: org/springframework/boot/maven/RepackageMojo has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version
of the Java Runtime only recognizes class file versions up to 55.0

My pom.xml πŸ‘‡

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.1</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>tuto.spring</groupId>
	<artifactId>security-filter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Hello bug</name>
	<description>Just test spring boot 3.0.1.</description>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

To fix this problem, I checked my java version (java -version [πŸ˜… This command was not necessary after the mvn -version]) and I realized that the active java version on my machine is version 11 while the version 3 of spring boot requires version 17.

Frist solution βœ…

The first solution that came to my mind is the modification of the environment variable on which maven is based to compile the project, in other words, modified the variable JAVA_HOME to make it point to the JDK 17 repository. To do this, I downloaded the JDK 17 and set JAVA_HOME to its repository (more details). After this step, I purged ⚠️ the dependencies from the local repository with the command mvn dependency:purge-local-repository [or remove the folder ${user.home}/.m2/repository] and perform mvn clean install. The safest way without performing a purge on the local repository is to use the -D mvn option with the command mvn -Dmaven.repo.local="path-for-custom-local-repository" clean install -DskipTests.

Second solution βœ…

The first solution worked for my new project but when I try to run old projects with the old version of spring, I had problems related to the version of Java, because I had them configured on JDK 11 (java verion 11) 😑. So, how could I have my Java 11 enabled on my windows 11 (:grin: I love 11) and still use my Java 17 in my new project? (:sob: this love took up half of my day). The first step is set JAVA_HOME point to the JDK 11 repository (more details). After this, I added java.version in pom.xml project properties πŸ‘‡

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
    <parent>
		...
        ...
	</parent>
    <properties>
            <java.version>17</java.version>
    </properties>
    ...
    ...
    <dependencies>
        ...
        ...
    </dependencies>
    <build>
        ...
        ...
    </build>
</project>

but the run output gave me β›” another issue πŸ‘‡

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project security-filter: Fatal error compiling: error: invalid target release: 17.

For this error, I change the version of my spring-boot-maven-plugin (I do this because spring-boot-maven-pluginspring-boot-maven-plugin:3.10.1 is built with java 17) πŸ‘‡

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
    <parent>
		...
        ...
	</parent>
    <properties>
            <java.version>17</java.version>
    </properties>
    ...
    ...
    <dependencies>
        ...
        ...
    </dependencies>
    <build>
        <plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.6.4</version>
            </plugin>
        </plugins>
    </build>
</project>

but I have the same error. [I read an article where it worked with this modification combined with the use of profiles configuration. link.]

The first good solution I had for my problem is the use of maven-toolchains-plugin ( Guide to Using Toolchains or Apache Maven Toolchains Plugin 😎). To implement this, I created a toolchain.xml file in my ${user.home}/.m2 folder and configured all my jdk there like this πŸ‘‡

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
    <!-- JDK toolchains -->
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>1.8</version>
        </provides>
        <configuration>
            <jdkHome>C:\Program Files\Java\jdk1.8.0_202</jdkHome>
        </configuration>
    </toolchain>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>10</version>
        </provides>
        <configuration>
            <jdkHome>C:\Program Files\Java\jdk-10.0.2</jdkHome>
        </configuration>
    </toolchain>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>11</version>
        </provides>
        <configuration>
            <jdkHome>C:\Program Files\Java\jdk-11.0.15.1</jdkHome>
        </configuration>
    </toolchain>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>17</version>
        </provides>
        <configuration>
            <jdkHome>C:\Program Files\Java\jdk-17.0.4</jdkHome>
        </configuration>
    </toolchain>
</toolchains>

After the creation and configuration of toolchain.xml, I added the following lines in my pom.xml file to configure the maven-toolchains-plugin πŸ‘‡

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
    <parent>
		...
        ...
	</parent>
    <properties>
            <!--java.version can be commented-->
            <java.version>17</java.version>
    </properties>
    ...
    ...
    <dependencies>
        ...
        ...
    </dependencies>
    <build>
        <plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
                <!--The version of spring-boot-maven-plugin must be specified at 2.6.3-->
				<version>2.6.4</version>
            </plugin>
            <!--Configure the maven-toolchains-plugin-->
            <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-toolchains-plugin</artifactId>
				<version>1.1</version>
				<executions>
					<execution>
						<phase>validate</phase>
						<goals>
							<goal>toolchain</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<toolchains>
						<jdk>
							<version>17</version>
						</jdk>
					</toolchains>
				</configuration>
			</plugin>
        </plugins>
    </build>
</project>

and the build worked πŸ‘‡

...
...
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.851 s - in tuto.spring.securityfilter.AuthenticationSecurityFilterApplicationTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
...
...
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS πŸŽ‰
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.141 s
[INFO] Finished at: 2023-01-14T02:48:30-05:00
[INFO] ------------------------------------------------------------------------

Remarks about the pom.xml:

  • <java.version>17</java.version> can be commented
  • If you do not specify a version of the spring-boot-maven-plugin less than 3.10.1 (I used version 2.6.4), you will get β›” another error πŸ‘‡
[WARNING] Error injecting: org.springframework.boot.maven.RepackageMojo
java.lang.TypeNotPresentException: Type org.springframework.boot.maven.RepackageMojo not present
...
...
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: java.lang.UnsupportedClassVersionError: org/springframework/boot/maven/RepackageMojo has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of
 the Java Runtime only recognizes class file versions up to 55.0
    at java.lang.ClassLoader.defineClass1 (Native Method)
    at java.lang.ClassLoader.defineClass (ClassLoader.java:1016)
...
...
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.895 s
[INFO] Finished at: 2023-01-14T01:59:37-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project security-filter: Fatal error compiling: error: invalid target release: 17 -> [H
elp 1]

Third solution βœ…

With the pom.xml that I have at the beginning of the problem, executing the following

mvn clean install -Dmaven.compiler.fork=true  -Dmaven.compiler.executable="C:\Program Files\Java\jdk-17.0.4\bin\javac" -Dmaven.repo.local="path-for-custom-local-repository" -DskipTests

and by specifying the version 2.6.4 for my spring-boot-maven-plugin, the build works πŸ”“.

Honorable Mention (What I would have applied if it didn't work πŸ‘€)

https://www.happycoders.eu/java/how-to-switch-multiple-java-versions-windows/

Useful links

https://dzone.com/articles/guide-for-supporting-multiple-versions-of-java-8-1

https://vladmihalcea.com/different-java-main-test-maven/

https://javarevisited.blogspot.com/2018/12/how-to-fix-invalid-target-release-17-18-error-in-maven.html

https://www.concretepage.com/build-tools/maven/activate-maven-profile-java-version

https://www.marcobehler.com/guides/a-guide-to-java-versions-and-features

https://books.sonatype.com/mvnref-book/reference/profiles.html

https://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html#resource-filtering-sect-system-properties

https://books.sonatype.com/mvnref-book/reference/running-sect-options.html

https://howtodoinjava.com/maven/change-local-repository-location/

https://spigandoeaprendendo.wordpress.com/2022/03/08/java-11-maven-spring-boot-erro-repackage-failed-unable-to-load-the-mojo-repackage-in-the-plugin-spring-boot-maven-plugin-due-to-an-api-incompatibility/

@ibrahim00017
Copy link

great job @welmarr
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment