What is the difference between `-DskipTests` and `-Dmaven.test.skip=true`, and how do `-D` properties work on the mvn command line in general?
answer
- skipTests = compile, don't run
- maven.test.skip = don't even compile
- -D sets a property, overrides POM
- skipITs = failsafe only
- CI must NOT skip
basics
~10 s-DskipTests compiles tests but skips running them. -Dmaven.test.skip=true skips both compiling AND running tests. -D sets a system/user property that plugins and the POM can read.
solid answer
~40 s`-Dname=value` on the command line sets a property Maven exposes to the POM (via `${name}`) and to plugins. For tests, two distinct properties exist. `-DskipTests` (read by Surefire/Failsafe) still **compiles** the test sources but does not **execute** the tests. `-Dmaven.test.skip=true` is stronger: it skips both test **compilation** and **execution** — useful when test code won't even compile or you need a fast artifact. Practically, prefer `-DskipTests` so tests stay compilable; reserve `maven.test.skip` for emergencies. Note `-DskipTests` only governs Surefire/Failsafe execution; `maven.test.skip` is also honored by the compiler plugin. These are command-line overrides of properties you could equally set in `<properties>` in the POM or in a profile.
code
bash · 11 lines# Compile tests but don't run them
mvn install -DskipTests
# Don't compile OR run tests (faster, riskier)
mvn install -Dmaven.test.skip=true
# Skip only integration tests (Failsafe)
mvn verify -DskipITs
# Generic property override consumed by the POM via ${env}
mvn package -Denv=prodgo deeper
Know both flags skip running tests.
Explain compile-vs-execute difference and that -D overrides POM properties.
Advise when each is appropriate and warn against masking compile failures.
Set org policy: local convenience flags allowed, canonical/release builds never skip tests.
## How `-D` works On the mvn command line, `-Dname=value` defines a **property**. Maven merges these into the property set available as `${name}` in the POM and passes them to plugins. (Bare `-Dname` sets it to `true`.) These are how you parameterize a build without editing files — they override matching `<properties>` entries in the POM for that single invocation. Example: if your POM has `<properties><env>dev</env></properties>`, running `mvn -Denv=prod ...` makes `${env}` resolve to `prod` for that run. ## Two ways to skip tests Testing in Maven is done by two plugins: **maven-surefire-plugin** (unit tests, `test` phase) and **maven-failsafe-plugin** (integration tests, `integration-test`/`verify`). ### `-DskipTests` - Recognized by **both Surefire and Failsafe**. - **Test sources are still compiled** (`test-compile` runs normally). - Tests are simply **not executed**. - Best default when you want a build but skip running tests, because it keeps test code compiling — broken test code still fails the build. ### `-Dmaven.test.skip=true` - Honored by Surefire/Failsafe **and the maven-compiler-plugin**. - **Skips compiling test sources entirely** AND skips running them. - Faster, but dangerous: test code is never compiled, so rot goes unnoticed. ### Failsafe-specific `-DskipITs` skips only integration tests (Failsafe) while still running unit tests. ## Guidance Use `-DskipTests` for routine 'build the jar without waiting on tests'. Use `-Dmaven.test.skip=true` only when test sources can't compile or for a throwaway fast build. Never bake either into CI for the canonical build — CI must run tests. ## Related properties Many plugins expose skip-style properties, e.g. `-Dmaven.javadoc.skip=true`, `-Dcheckstyle.skip=true`.
- Why might `-DskipTests` be preferred over `maven.test.skip` in day-to-day use?It still compiles test sources, so test code that no longer compiles will fail the build — you don't silently let tests rot.
- Which plugins read these properties?`-DskipTests` is read by Surefire and Failsafe; `-Dmaven.test.skip` is additionally honored by the compiler plugin to skip test compilation.
saying these in an interview costs you the question
- Claiming the two flags are equivalent — they differ on whether test code is compiled.
- Saying `-DskipTests` speeds up by skipping compilation (it doesn't skip compilation).
- Putting test-skip flags in the canonical CI build.