Run the JUnit tests in Maven without building and copying files


This question already has an answer here:

  • How can I atomically run mvn tests (without rebuilding source code)? 3 answers

I have a large Maven project which has several modules in it. When I want to run a JUnit test from one module I run 'mvn -Dtest=NameOfTest test' in the directory that contains all the modules. When I run this command Maven goes through each module and tries to compile it (though it's already compiled), which involves copying a bunch of files and adds to the total time of the test. It seems that the 'test' command for the Maven surefire plugin executes all the steps up to the test. I was wondering if there is a way to execute only the test step and not bother with all the attempted compilation and copying of files.

Here is some output from before the test starts:

[INFO] --- build-helper-maven-plugin:1.5:add-test-source (add-test-source) @ module1 ---
[INFO] Test Source directory: <directory in module1 with some generated sources> added.
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ module1 ---
[debug] execute contextualize
[INFO] Copying 108 resources
[INFO] Copying 1113 resources
[INFO] Copying 1 resource

It repeats this for each of the other modules. All told it takes a minute or two before it actually starts the test. Does anyone know a way to get the test to run without bothering with all the compilation beforehand? Please let me know if there's any more information I should provide.

If what you would like to do is to run just the test phase of the lifecycle without running all of the previous phases you could also call the goal that is bound to the test phase:

mvn surefire:test

or if you want to run just one test

mvn -Dtest=NameOfTest surefire:test