问题描述
默认情况下,maven surefile插件在隔离(分叉)环境中运行测试.您可以使用以下配置覆盖此行为:
By default maven surefile plugin run tests in isolated (forked) environment. You can override this behavior with following configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>never</forkMode>
</configuration>
</plugin>
</plugins>
</build>
如果您需要调试测试,则应使用此配置代码段.或者,您可以简单地通过以下方式运行maven build:
If you need to debug your tests you should to use this configuration snippet. Or you could simply run maven build the following way:
$ mvn -Dmaven.surefire.debug tests
这将在端口5005上启动调试器.
This will starts a debugger on the port 5005.
我的问题是:分叉策略有什么好处,为什么选择Maven构建作为默认策略?非分叉策略不是更直接,因此应将其用作默认策略(专家是配置工具上的约定,对吗)?
My question is: what benefits have forking strategy and why is chosen as a default strategy for maven build? Isn't nonforking strategy is more straightforward and therefore should be used as default (maven is convention over configuration tool, right)?
推荐答案
默认情况下,Surefire使用仅清单文件的JAR进行测试. IMO,主要优点是:
By default, Surefire forks your tests using a manifest-only JAR. IMO, the main advantages are that:
- 它为隔离的环境提供了某种正确"的类路径.
- 它保护Maven进程本身(这是一件好事,特别是如果Maven正在嵌入式运行在您的IDE中).
直截了当的目的是什么?在IDE中轻松调试?我相信这并不是最初的意图(并且我更愿意在需要时连接远程调试器并保持Maven主过程的安全).
Straightforward for what? Easy debugging inside an IDE? I believe that was not the initial intention (and I prefer to connect a remote debugger if the need arises and to keep the main Maven process safe).
这篇关于Maven surefire插件分叉模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!