63

Running maven (3.5.2) build of a Spring Boot 2.0.2.RELEASE applicaton (generated by web initialiser with web dependencies) fails executing the maven-surefire-plugin saying just:

Error: Could not find or load main class org.apache.maven.surefire.booter.ForkedBooter

Caused by: java.lang.ClassNotFoundException: org.apache.maven.surefire.booter.ForkedBooter

Why is this happening? Is it a problem in boot + surefire integration = a bug?

For reference, the dependencies that seem relevant are:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/>
</parent>
...
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
</dependency>
...
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
jediz
  • 4,459
  • 5
  • 36
  • 41
  • The [upstream issue](https://issues.apache.org/jira/browse/SUREFIRE-1588) shows three workarounds (the two listed here, plus `forkCount` 0), but none are without problems ☹ – mirabilos Oct 31 '18 at 14:44

6 Answers6

132

Workaround for the issue was to override Spring Boot's maven-surefire-plugin definition and set useSystemClassLoader to false. Read Surefire docs for more details

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>
    </plugins>
</build>
jediz
  • 4,459
  • 5
  • 36
  • 41
  • 3
    Yes, I can confirm the solutions fixes the problem, but what is the issue behind the scenes? - The nightly builds on our CI system were suddenly red without any code changes. Is it an issue with Spring Boot? – agassner Oct 29 '18 at 16:47
  • 5
    @agassner take a look at this, I arrived here by [this](https://stackoverflow.com/a/53016532/865910) post – Ordiel Oct 29 '18 at 19:11
  • As said in [the other answer](https://stackoverflow.com/a/53083806/2171120) and [the upstream issue](https://issues.apache.org/jira/browse/SUREFIRE-1588) this workaround is not without problems. – mirabilos Oct 31 '18 at 14:43
  • 2
    This fix worked for me, however, I got a warning that a version should be supplied/indicated. To avoid future errors I'd recommend adding one. As of now, 2.22.1 seems to be the latest. – Mark Nov 01 '18 at 11:02
  • one should set the version not to get maven warnings on build. edit: as still said in previuous comment – Kaspatoo Nov 21 '18 at 13:25
  • @Kaspatoo the version was inherited from Spring boot, which is one of it's main features - it's supposed to define the compatible versions for your. – jediz Nov 21 '18 at 23:21
21

The <useSystemClassLoader>false</useSystemClassLoader> solution provideded by jediz did allow my surefire tests to run, but broke class loading in some of my Spring Boot integration tests.

The following maven-surefire-plugin configuration worked for me:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
    </configuration>
</plugin>
Andreas Presthammer
  • 1,886
  • 2
  • 19
  • 31
15

This is due to a known bug in the Maven Surefire plugin. It was fixed in version 3.0.0-M1, which was released in November 2018. So the simplest and most reliable fix is to upgrade which version of the plugin you use.

Updating the maven-surefire-plugin from 2.12.4 to 3.0.0-M1 worked for me. The project did not explicitly use the plugin, so I had to add a new plugin dependency.

<plugins>
   ...
   <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M1</version>
   </plugin>
   ...
</plugins>
Raedwald
  • 46,613
  • 43
  • 151
  • 237
rvange
  • 2,432
  • 2
  • 20
  • 23
11

To me, the solution was to run mvn as

_JAVA_OPTIONS=-Djdk.net.URLClassPath.disableClassPathURLCheck=true mvn clean compile package

Other ideas (giving the system property to the maven argument list, different changes in pom.xml, settings.xml) did not work.

Despite that it didn't contain the exact solution, also this answer was very helpful for me to make it clear, that it is an unfortunate cooperation of two independent, alone harmless bugs in the Ubuntu JDK and the Maven Surefire Plugin.

Recent Debian (buster) with the same JDK and Maven versions doesn't seem affected by the problem, but Ubuntu (xenial) did.

The exact solution is coming from this answer.

Update from the future: with Debian Buster is alles okay and this workaround is not needed any more.

peterh
  • 11,875
  • 18
  • 85
  • 108
1

I was able to remove the maven-surefire-plugin from my POM after adding this to the top of my POM (inside the <project> node)

<prerequisites>
    <maven>3.6.3</maven>
</prerequisites>

Why do I think this is the right answer?

  • It specifies the version of Maven that Maven recommends using: https://maven.apache.org/download.cgi
  • when you run mvn versions:display-plugin-updates it shows that it's taking the maven-surefire-plugin 3.0.0-M3 from super-pom, which so far seems to have this issue fixed.
  • You don't have to manage individual plugin versions independently going forward. Just your minimum maven version which controls the super-pom version.
GlenPeterson
  • 4,866
  • 5
  • 41
  • 49
  • 1
    I tried this solution and here's what I got: `[WARNING] The project com.example:pom:0.0.2 uses prerequisites which is only intended for maven-plugin projects but not for non maven-plugin projects. For such purposes you should use the maven-enforcer-plugin. See https://maven.apache.org/enforcer/enforcer-rules/requireMavenVersion.html` – Raymund Arthur May 14 '19 at 07:09
  • i tested it with maven 3.6.0, OpenJDK11, and Spring boot 2.1.X parent. I managed to solve the issue by using [rvange](https://stackoverflow.com/users/512135/rvange)'s [answer](https://stackoverflow.com/a/53261434/8687642) above. – Raymund Arthur May 29 '19 at 04:24
  • I'm sorry that my answer didn't work for you, but glad that you found one that did! My answer is still working well for me though, on several projects, so I'm going to leave it there. – GlenPeterson May 30 '19 at 15:21
1

Adding this to the maven-surefire-plugin I resolved the problem:

<plugin>    
  <groupId>org.apache.maven.plugins</groupId>   
  <artifactId>maven-surefire-plugin</artifactId>    
  <configuration>
    <forkCount>0</forkCount>
  </configuration>
</plugin>
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 1
    Welcome to SO! When answering, please do more than toss out code. Instead, explain why the code should be the selected solution. The goal is to educate, to teach the OP how to fish, not just solve this current problem with a single fish. – the Tin Man Mar 23 '20 at 21:57
  • I've only seen the need for this when running an older version of the plugin under a newer version of Java, like 11. Try the latest version of the plugin instead. – Matthew Read Mar 18 '21 at 18:40