/compare/maven/skiptests-vs-maven-test-skip
mvn -DskipTests vs -Dmaven.test.skip=true: what each one skips
- tool
- Maven
- compares
- -DskipTests vs -Dmaven.test.skip=true
mvn package -DskipTests compiles the test sources but does not run them. mvn package -Dmaven.test.skip=true skips both compiling and running tests, so it is faster but also breaks anything that needs the compiled test classes, such as a test-jar consumed by another module. Use -DskipTests by default, and -Dmaven.test.skip=true only when the test code itself does not compile or is not needed at all.
Side by side
| Criterion | -DskipTests | -Dmaven.test.skip=true |
|---|---|---|
| Compiles src/test/java (testCompile) | yes | no |
| Copies test resources | yes | no |
| Runs unit tests (Surefire) | no | no |
| Runs integration tests (Failsafe) | older Failsafe: no; Failsafe 3.6.0: yes, unless -DskipITs | no |
| test-jar for other modules | built with the compiled test classes | missing classes; dependent modules can fail |
| Catches test code that no longer compiles | yes | no |
| Typical use | Fast local packaging, deploy jobs after a separate test job | Emergency builds when test code is broken |
Commands
mvn package -DskipTests # compile tests, do not run them
mvn package -Dmaven.test.skip=true # do not even compile tests
mvn verify -DskipITs # run unit tests, skip Failsafe integration tests
mvn test -Dtest=OrderServiceTest # run a single test class instead of skipping
mvn test -Dsurefire.failIfNoSpecifiedTests=false -Dtest='Order*' # pattern, no failure if a module has noneSetting it in pom.xml
<properties>
<!-- default for this project; override with -DskipTests=false -->
<skipTests>true</skipTests>
</properties>What each property actually does
skipTests is a parameter of the Surefire plugin: the test phase still runs every step before it (compiling main code, copying test resources, compiling tests), and only the execution is skipped. maven.test.skip is read by more plugins: the Compiler Plugin skips testCompile, the Resources Plugin skips testResources, and Surefire and Failsafe skip execution. Nothing in target/test-classes is produced.
| Property | Read by | Effect |
|---|---|---|
| skipTests | Surefire (and Failsafe before 3.6.0) | Do not run tests; still compile them |
| maven.test.skip | Compiler, Resources, Surefire, Failsafe | Do not compile or run tests |
| skipITs | Failsafe | Do not run integration tests; unit tests still run |
| maven.test.skip.exec | Surefire, Failsafe | Older, deprecated way to skip execution only |
Which one to use
- Default choice:
-DskipTests. The build stays honest about test code that no longer compiles, and modules that publish atest-jarkeep working. - Integration tests only:
-DskipITskeeps unit tests and skips the slow Failsafe suite, for example on every push, with the fullmvn verifyon a schedule or before release. - Test code is broken and you need an artifact now:
-Dmaven.test.skip=true, as a one-off. Fix the tests before the next release. - Never by default in CI: a pipeline that deploys should run at least one full
mvn verify.
FAQ
Does -DskipTests skip integration tests?
It depends on the Failsafe version. Older Failsafe releases bound their skipTests parameter to the same property, so -DskipTests skipped both. The Failsafe 3.6.0 documentation says the parameter is no longer bound to it: use -DskipITs, or -Dmaven.test.skip=true to skip everything.
Why does my build fail with -Dmaven.test.skip=true but work with -DskipTests?
Something needs the compiled test classes, typically a module that depends on another module's test-jar. With maven.test.skip the classes are never compiled, so that jar is empty or missing.
How do I run tests again when skipTests is true in the pom?
Pass -DskipTests=false on the command line; a user property overrides the value set in the properties section.