nephewtom
nephewtom

Reputation: 3121

mvn test vs mvn surefire:test

I am running a single test with Maven.

Which is the difference of running it in these two ways:

mvn -Dtest=DatabaseTest test

mvn -Dtest=DatabaseTest surefire:test

I can see that test shows:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ rac.mybatis ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ rac.mybatis ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ rac.mybatis ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/etomort/smip/oracle-rac/mybatis-rac/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ rac.mybatis ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ rac.mybatis ---
[INFO] Surefire report directory: /home/etomort/smip/oracle-rac/mybatis-rac/target/surefire-reports

While surefire:test shows:

[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ rac.mybatis ---
[INFO] Surefire report directory: /home/etomort/smip/oracle-rac/mybatis-rac/target/surefire-reports

This question has very valuable explanation, though current one points to a typical doubt one can face and it is a very concrete use case. The answer provided by @Shadow clarifies it. The maven-lifecycle-vs-phase-vs-plugin-vs-goal question can be consulted to widen and for deeper understand of Maven way of working.

Upvotes: 5

Views: 3337

Answers (2)

oberlies
oberlies

Reputation: 11723

The difference is that test runs the complete build up to the points where tests are executed, whereas surefire:test only executes the tests, re-using intermediate results from a previous build.

So if you e.g. edit a test and only run surefire:test, the new version of the test will not be compiled and Maven will therefore execute some old version of the test. In general, it will be hard to say which version of the test will be executed in this way, so this is why people in general use mvn clean test instead of mvn surefire:test.

Upvotes: 6

Shadow
Shadow

Reputation: 321

test is a lifecycle phase and since Maven is convention based, it means that by default there are certain plugins already bound to each phase.

You can also add your own to a phase but you can also invoke the goal of a plugin manually mvn plugin:goal.

Surefire is the official test plugin, bound to the test phase.

Upvotes: 2

Related Questions