Maven Surefire 插件 fork 模式
默认情况下,maven Surefile 插件在隔离(分叉)环境中运行测试。您可以使用以下配置覆盖此行为:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>never</forkMode>
</configuration>
</plugin>
</plugins>
</build>
如果您需要调试测试,您应该使用此配置片段。或者您可以简单地按以下方式运行 maven build:
$ mvn -Dmaven.surefire.debug tests
这将在端口 5005 上启动调试器。
我的问题是:分叉策略有什么好处以及为什么选择它作为 maven build 的默认策略?非分叉策略不是更简单,因此应该用作默认策略(maven 是约定优于配置工具,对吧)?
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>
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
This will starts a debugger on the port 5005.
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,Surefire 使用仅包含清单的 JAR 来分叉您的测试。 IMO,主要优点是:
直截了当干什么?在 IDE 中轻松调试?我相信这不是最初的意图(如果需要的话,我更喜欢连接远程调试器并保持主 Maven 进程的安全)。
另请参阅
By default, Surefire forks your tests using a manifest-only JAR. IMO, the main advantages are that:
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).
See also
如果将分叉模式设置为“true”或“once”,则分叉模式有助于加载系统类路径。
但有时如果使用 maven-surefire 2.5 插件,设置 fork mode= true 会导致“命令行太长”或“测试失败”等错误。为了避免此错误,建议使用 suefire 插件 2.5 时设置 ser forkmode=never
forking mode helps to load system classpath if it is set to "true" or "once".
But sometimes setting fork mode= true caues errors like "commmand line too long" or "there are test failures" if maven-surefire 2.5 plugin is used. To avoid this error its is recommended to ser forkmode=never whn using suefire plugin 2.5