By default, sbt executes the
test tasks within its own JVM instance.
It emulates running an external java command by invoking the task in an isolated
ClassLoader. Compared to forking, this approach reduces the start
start up latency and total runtime. The performance benefit from simply reusing
the JVM is modest. Class loading and linking of the application dependencies
dominate the start up time of many applications. sbt reduces this start up
latency by re-using some of the loaded classes between runs. It does this by
creating a layered
ClassLoader following the standard delegation model of a java
The outermost layer, which always contains the class files and jars specific to
the project, is discarded between runs. The inner layers, however, can be
Starting with sbt 1.3.0, it is possible to configure the particular approach
that sbt takes to generate layered
ClassLoader instances. It is specified via
classLoaderLayeringStrategy. There are three possible values:
ScalaLibrary- The parent of the outermost layer is able to load the scala standard library as well as the scala reflect library provided it is on the application classpath. This is the default strategy. It is most similar to the layered
ClassLoadersprovided by sbt versions < 1.3.0.
AllLibraryJars- Adds an additional layer for all of the dependency jars between the scala library layer and the outermost layer. It is the default strategy when turbo mode is enabled. This strategy can significantly improve the startup and total runtime performance compared to
ScalaLibrary. Results may be inconsistent if any of the libraries have mutable global state because, unlike
ScalaLibrary, the global state persists between runs. When any libraries use java serialization,
AllLibraryJarsshould be avoided.
fullClasspathkey of the task is loaded in the outermost layer. Consider using as an alternative to fork if any issues are experienced with
ScalaLibraryor if the application requires all classes to be loaded in the same
ClassLoader, which may be the case for some uses of java serialization.
classLoaderLayeringStrategy can be set in different configurations. For
example, to use the
AllLibraryJars strategy in the
Test configuration, add
Test / classLoaderLayeringStrategy := ClassLoaderLayeringStrategy.AllLibraryJars
build.sbt file. Assuming no other changes to the
build.sbt file, The
run task will still use