skip to content

What is the difference between -DskipTests and -Dmaven.test.skip, and when would you use each?

level: middleimportance: must knowfreq 72%

answer

  1. skipTests = compile yes, run no
  2. maven.test.skip = compile no, run no
  3. compiler plugin reads maven.test.skip
  4. testFailureIgnore = run but don't fail
  5. prefer skipTests

basics

~20 s

-DskipTests compiles test classes but does not run them. -Dmaven.test.skip=true skips both compiling and running tests. Use skipTests to build a jar fast while keeping tests compilable; use maven.test.skip only when you also want to skip test compilation.

solid answer

~40 s

Both let you bypass tests, but at different stages. -DskipTests (a Surefire property) still runs the test-compile phase, so your test sources are compiled and validated; Surefire just doesn't execute them. -Dmaven.test.skip=true is broader: it tells maven-compiler-plugin to skip compiling test sources AND tells Surefire not to run, so tests are neither compiled nor executed. Prefer -DskipTests for routine fast packaging because you still catch compile errors in tests; reach for -Dmaven.test.skip only when test compilation itself is slow or broken and you knowingly accept that risk. You can also set <skipTests>true</skipTests> in the plugin config or as a property. There's also testFailureIgnore, which runs tests but doesn't fail the build on failures - a different concept entirely.

code

bash · 2 lines
bash
mvn package -DskipTests          # tests compiled, not run
mvn package -Dmaven.test.skip=true  # tests not compiled, not run

go deeper

for a junior

Know -DskipTests skips running tests for a fast build.

for a middle

Articulate that maven.test.skip also skips test compilation while skipTests does not.

for a senior

Choose deliberately and explain the compile-safety trade-off; never let skip flags leak into CI.

for a principal

Set org policy: CI never skips; reserve maven.test.skip for documented emergency paths and guard it via profiles.

## The two flags | Flag | Compiles test sources? | Runs tests? | |------|------------------------|-------------| | `-DskipTests` | Yes (test-compile runs) | No | | `-Dmaven.test.skip=true` | No | No | ### -DskipTests This is a property honored by the Surefire (and Failsafe) plugin. The `test-compile` phase still runs, so `src/test/java` is compiled and any **compile errors in tests still break the build**. Only the *execution* is skipped. This is the safe choice for "I just need the artifact now". ```bash mvn package -DskipTests ``` ### -Dmaven.test.skip This property is read by both the `maven-compiler-plugin` (skip `testCompile` goal) and Surefire (skip run). So test sources are **not compiled at all**. Faster, but you lose any signal that your tests even compile against the current code. ```bash mvn install -Dmaven.test.skip=true ``` ## In the POM ```xml <properties> <maven.test.skip>false</maven.test.skip> </properties> <!-- or on the plugin --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>${skip.unit.tests}</skipTests> </configuration> </plugin> ``` ## Don't confuse with testFailureIgnore `-Dmaven.test.failure.ignore=true` (or `<testFailureIgnore>true</testFailureIgnore>`) actually **runs** the tests but does not fail the build when some fail - useful for collecting all results in CI. That is the opposite of skipping. ## Guidance - Default CI: run everything. - Local fast iteration / packaging an artifact: `-DskipTests` (keeps test compilation honest). - Emergency build where test compilation is broken/slow and you accept the risk: `-Dmaven.test.skip=true`.

  • Which flag still catches a compile error in a test file?
    -DskipTests, because test-compile still runs. -Dmaven.test.skip skips compilation, so it would not.
  • How do you run tests but not fail the build on failures?
    Use -Dmaven.test.failure.ignore=true or <testFailureIgnore>true</testFailureIgnore>; this is distinct from skipping.

saying these in an interview costs you the question

  • Saying the two flags are equivalent.
  • Claiming -DskipTests skips test compilation.
  • Confusing skipping with testFailureIgnore (which still runs tests).

context