Question

How can I warm up the dependency cache of my maven tests? E.g. mvn test -DskipTests downloads some of the dependencies, but not all, e.g. some of the maven surefire plugin dependencies are only downloaded by mvn test.

I want to create a snapshot of my filesystem to boost up the execution of my tests. Therefore I'd like to have all the dependencies downloaded, but I want to achieve this without executing the tests itself.

Some of the dependencies that are only downloaded during mvn test and not by mvn test -DskipTests:

Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-junit47/2.14/surefire-junit47-2.14.pom
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-    junit47/2.14/surefire-junit47-2.14.pom (4 KB at 13.9 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-providers/2.14/surefire-providers-2.14.pom
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-
Was it helpful?

Solution

The dependencies are resolved on a need-by-need basis:

  • The compile time ones for the application's code are downloaded as a very first step when you start the build.

  • Any plugins required are downloaded afterwards during the respective phases. This also includes their transitive dependencies.

  • The dependencies for the tests are downloaded when the tests are to be compiled and executed. These are the dependencies with <scope>test</scope>.

Therefore, at the point where you're at the test phase, you already must have the latest dependencies, unless you have them cached locally and installed and you're in offline mode.

To resolve all your dependencies, you can do:

mvn dependency:go-offline

To resolve all the plugins, you can do:

mvn dependency:resolve-plugins

OTHER TIPS

maven surefire plugin gets downloaded only for mvn test because default Goal execution phase for this plugin is test.

I guess if you want to execute surefire plugin you can specify goal execution phase.

We have the following approach for filling the Maven cache on our Jenkins slaves:

  • Do a full build
  • Zip the ~/.m2 folder (which contains the cached instances)
  • Place it and extract it on the machine where you want to run your "warmed-up" tests
  • Automate steps above steps to keep it recent and up to date.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top