Spring Boot无法运行maven-surefire-plugin ClassNotFoundException org.apache.maven.surefire.booter.ForkedBooter

jed*_*diz 54 spring maven maven-surefire-plugin spring-boot

运行maven(3.5.2)构建Spring Boot 2.0.2.RELEASE应用程序(由Web初始化程序生成,具有Web依赖性)无法执行maven-surefire-plugin说:

错误:无法找到或加载主类org.apache.maven.surefire.booter.ForkedBooter

引起:java.lang.ClassNotFoundException:org.apache.maven.surefire.booter.ForkedBooter

为什么会这样?这是boot + surefire集成中的一个问题=一个bug?

作为参考,似乎相关的依赖关系是:

<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>
Run Code Online (Sandbox Code Playgroud)

jed*_*diz 121

该问题的解决方法是覆盖Spring Boot的maven-surefire-plugin定义并设置useSystemClassLoaderfalse.阅读Surefire文档了解更多详情

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

  • @agassner看看这个,我来到[这里](/sf/answers/3711157271/)帖子 (5认同)
  • 是的,我可以确认解决方案解决了问题,但幕后问题是什么? - 我们的CI系统上的夜间构建突然变红,没有任何代码更改.这是Spring Boot的问题吗? (2认同)
  • 此修复程序对我有用,但是,我收到警告,应提供/指示版本。为了避免将来出现错误,我建议添加一个。截至目前,&lt;version&gt;2.22.1&lt;/version&gt; 似乎是最新的。 (2认同)

And*_*mer 17

<useSystemClassLoader>false</useSystemClassLoader>由jediz提供的解决方案确实允许我的surefire测试运行,但在我的一些Spring Boot集成测试中打破了类加载.

以下maven-surefire-plugin配置对我有用:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)


pet*_*ica 10

对我来说,解决方案是运行mvn as

_JAVA_OPTIONS=-Djdk.net.URLClassPath.disableClassPathURLCheck=true mvn clean compile package
Run Code Online (Sandbox Code Playgroud)

其他的想法(给系统属性Maven的参数列表,在不同的变化pom.xml,settings.xml)没有工作.

尽管它没有包含确切的解决方案,但这个答案对我来说非常有帮助,但是在Ubuntu JDK和Maven Surefire插件中它是两个独立的,无害的错误的不幸合作.

最近使用相同JDK和Maven版本的Debian(buster)似乎没有受到这个问题的影响,但是Ubuntu(xenial)做到了.

确切的解决方案来自这个答案.


rva*_*nge 9

将maven-surefire-plugin从2.12.4更新到3.0.0-M1对我有用.该项目没有明确使用该插件,因此我不得不添加一个新的插件依赖项.

<plugins>
   ...
   <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M1</version>
   </plugin>
   ...
</plugins>
Run Code Online (Sandbox Code Playgroud)