Search code examples
javamavenjunit5spring-boot-maven-pluginjava-12

Maven surefire plugin 2.22.2 build fails for Junit 5 test cases with errror Unsupported class file major version 56 in spring boot


Junit 5 test cases for a Spring boot application 2.1.7.RELEASE fails build with below error in intelij.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project domain: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test failed: Unsupported class file major version 56 -> [Help 1]

using java 9 modularization

Application builds in maven and works without test cases.

used maven surefire plugin version 2.22.1 fails in command line and intellij

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>



        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <jvmArguments>--add-modules=java.xml.ws.annotation</jvmArguments>
            </configuration>
        </plugin>

Solution

  • From the error log you can see that the sub-modules are using maven-surefire-plugin 2.22.2 The solution for this is defining the version of Maven plugins to be used by all modules adding this section to the main pom.xml:

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.1.2</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <release>12</release>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    

    I could compile it after removing the Oracle dependency, I don't know if you will need to do that. If it works for you please comment in the issue.