thatonetallguy
thatonetallguy

Reputation: 51

Force maven to run tests in a Spring Boot app with a specific jdk

I'm running Ubuntu 22.04 right now, and trying to use maven toolchains to compile a Java 17 Spring Boot project while my active jdk is JDK11. I set up my toolchains so they use JDK18 to compile it, and this works great. However, when I try to actually run the tests (with mvn clean install), I get the following error:

...ApplicationTests 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

Everything works out fine when I run mvn clean install -DskipTests. I can see the testCompile goal executing and the test classes being compiled. I'm guessing the issue is that, after compilation with JDK18, maven still tries to use JDK11 to actually run the tests. Any idea how I can tell maven to run the tests with the correct JDK, when I do mvn clean install or similar?

Edit: I managed to get it to work by hardcoding my JDK18 path for the maven surefire plugin. It looks like this:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <configuration>
                <jvm>/usr/lib/jvm/java-18-openjdk-amd64/bin/java</jvm>
            </configuration>
        </plugin>

While this is alright, I don't like the hardcoding. According to the documentation (seems out of date, but eh), maven-surefire-plugin should be toolchain aware, and should be able to work if I specify my toolchain with something like

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <configuration>
                <jdkToolchain>
                    <version>18</version>
                    <vendor>sun</vendor>
                </jdkToolchain>
            </configuration>
        </plugin>

or not even that, since surefire should be able to derive this from the toolchain I configure with the maven-toolchains-plugin. Relevant toolchain config, just for the record:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-toolchains-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>toolchain</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <toolchains>
                    <jdk>
                        <version>18</version>
                        <vendor>sun</vendor>
                    </jdk>
                </toolchains>
            </configuration>
        </plugin>

Upvotes: 0

Views: 825

Answers (0)

Related Questions