Maven surefire plugin fork mode

Denis Bazhenov picture Denis Bazhenov · Jul 6, 2010 · Viewed 31.1k times · Source

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

Answer

Pascal Thivent picture Pascal Thivent · Jul 6, 2010

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