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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50661648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:37:14  来源:igfitidea点击:

Spring Boot fails to run maven-surefire-plugin ClassNotFoundException org.apache.maven.surefire.booter.ForkedBooter

springmavenspring-bootmaven-surefire-plugin

提问by jediz

Running maven (3.5.2) build of a Spring Boot2.0.2.RELEASE applicaton (generated by web initialiser with web dependencies) fails executing the maven-surefire-pluginsaying just:

运行Spring Boot2.0.2.RELEASE 应用程序的maven (3.5.2) 构建(由具有 web 依赖项的 web 初始化程序生成)无法执行maven-surefire-plugin说:

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

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

引起:java.lang。ClassNotFoundException:org.apache.maven.surefire.booter。分叉启动器

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

为什么会这样?启动+surefire集成=错误是否有问题?

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>

回答by jediz

Workaround for the issue was to override Spring Boot's maven-surefire-plugindefinition and set useSystemClassLoaderto false. Read Surefire docsfor more details

该问题的解决方法是覆盖 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>

回答by Andreas Presthammer

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.

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

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

以下 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>

回答by peterh - Reinstate Monica

To me, the solution was to run mvn as

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

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

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

Despite that it didn't contain the exact solution, also this answerwas 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.

尽管它没有包含确切的解决方案,但这个答案也对我非常有帮助,我清楚地表明,这是 Ubuntu JDK 和 Maven Surefire 插件中两个独立的、单独无害的错误的不幸合作。

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

最近具有相同 JDK 和 Maven 版本的 Debian (buster) 似乎没有受到这个问题的影响,但 Ubuntu (xenial) 却受到了影响。

The exact solution is coming from thisanswer.

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

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

来自未来的更新:使用 Debian Buster 一切正常,不再需要此解决方法。

回答by rvange

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.

将 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>

回答by GlenPeterson

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)

将它添加到我的 POM 顶部(<project>节点内部)后,我能够从我的 POM 中删除 maven-surefire-plugin

<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-updatesit 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.
  • 它指定了 Maven 推荐使用的 Maven 版本:https: //maven.apache.org/download.cgi
  • 当您运行mvn versions:display-plugin-updates它时,它表明它正在从 super-pom 获取 maven-surefire-plugin 3.0.0-M3,到目前为止似乎已经解决了这个问题。
  • 您不必独立管理各个插件版本。只是您控制超级 pom 版本的最小 Maven 版本。

回答by Ruben Romero

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

将此添加到 maven-surefire-plugin 我解决了问题:

<plugin>    
  <groupId>org.apache.maven.plugins</groupId>   
  <artifactId>maven-surefire-plugin</artifactId>    
  <configuration>
    <forkCount>0</forkCount>
  </configuration>
</plugin>