Maven surefire plugin fork mode
Asked Answered
D

2

28

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)?

Donkey answered 6/7, 2010 at 11:41 Comment(2)
Of note, forkMode is now deprecated, reuseForks should be used insteadGermin
Actually I think forkCount=0 is what should be used now to disable new JVMs launching. maven.apache.org/surefire/maven-surefire-plugin/…Prier
V
19

My question is: what benefits have forking strategy and why is chosen as a default strategy for maven build?

By default, Surefire forks your tests using a manifest-only JAR. IMO, the main advantages are that:

  1. it provides an isolated environment with a somehow "correct" classpath.
  2. it protects the maven process itself (which is a good thing, especially if Maven is running embedded in your IDE).

Isn't nonforking strategy is more straightforward and therefore should be used as default?

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

Vicar answered 6/7, 2010 at 13:33 Comment(3)
What does "forking a test" mean?Gamache
A fork is when a process executes a task in a new process rather than its own. In case of surefire, it means tests will be run in separate processes for maximum performance / concurrency. Sometimes you might want to tweak that or switch it off altogether (when debugging, for example).Natelson
well this makes testing in ide practically uselessSickening
G
1

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

Goop answered 20/10, 2010 at 19:45 Comment(1)
I think this sort of error happens only on Windows, because of this OS have some limitations on command line length.Donkey

© 2022 - 2024 — McMap. All rights reserved.