运行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>
答案 0 :(得分:115)
问题的解决方法是覆盖Spring Boot的maven-surefire-plugin
定义并将useSystemClassLoader
设置为false
。请阅读Surefire docs了解详情
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
答案 1 :(得分:17)
jediz提供的<useSystemClassLoader>false</useSystemClassLoader>
解决方案的确可以运行我的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>
答案 2 :(得分:10)
对我来说,解决方案是将mvn运行为
_JAVA_OPTIONS=-Djdk.net.URLClassPath.disableClassPathURLCheck=true mvn clean compile package
其他想法(将系统属性赋予maven参数列表,pom.xml
,settings.xml
中的不同更改)无效。
尽管它不包含确切的解决方案,但this answer对我来说也非常有帮助,因为它是Ubuntu JDK和Maven中两个独立的,无害的错误的不幸合作。 Surefire插件。
具有相同JDK和Maven版本的最近Debian(破坏者)似乎不受此问题影响,而Ubuntu(xenial)却受到了影响。
确切的解决方案来自this答案。
答案 3 :(得分: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>
答案 4 :(得分:1)
将此添加到maven-surefire-plugin中,我解决了该问题:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>0</forkCount>
</configuration>
</plugin>
答案 5 :(得分:0)
将其添加到POM顶部(在<project>
节点内部)后,便能够从POM中删除maven-surefire-plugin
<prerequisites>
<maven>3.6.1</maven>
</prerequisites>
为什么我认为这是正确的答案?
mvn versions:display-plugin-updates
时,它表明它正在从super-pom中获取maven-surefire-plugin 3.0.0-M3,到目前为止,该问题似乎已得到解决。