skip to content

Build Lifecycle & Invocation

Maven's build model: three lifecycles, ordered phases, and goals bound to those phases by packaging, all driven from the mvn command line. Interviewers probe here because most Maven confusion traces back to not knowing what `mvn package` actually executes.

on this pageshow

explore

questions

16

On the mvn command line, what is the difference between passing a lifecycle phase (like `mvn package`) and a direct plugin goal (like `mvn compiler:compile`)?

level: juniorimportance: must knowfreq 80%

answer

  1. phase runs up to and including
  2. goal = prefix:goal, runs alone
  3. left-to-right execution
  4. clean package = two lifecycles
  5. phases do nothing without bound goals

basics

~10 s

A phase (e.g. package) runs every step bound to it and all earlier phases in order. A goal (e.g. compiler:compile) runs just that one plugin task by itself.

solid answer

~40 s

Maven's command line accepts two kinds of arguments. A lifecycle phase like `package` is a named stage; running it executes all phases up to and including it (validate → compile → test → package), and at each phase Maven runs the plugin goals bound to it. A direct goal is written `prefix:goal` (e.g. `compiler:compile`) or fully as `groupId:artifactId[:version]:goal`; it runs exactly that one goal and nothing else — no preceding phases. You normally invoke phases for the standard build flow and invoke a single goal ad hoc (e.g. `mvn dependency:tree`, `mvn help:effective-pom`). You can also mix them on one line: `mvn clean package` runs the clean phase then the default-lifecycle build, and arguments execute left to right.

code

bash · 9 lines
bash
# Phase: runs validate->compile->test->package
mvn package

# Direct goal: runs ONLY this goal, nothing before it
mvn compiler:compile
mvn dependency:tree

# Mixed, executed left to right
mvn clean install

go deeper

for a junior

Know that a phase runs everything up to it; a goal is one task.

for a middle

Explain goal binding to phases and prefix:goal vs fully-qualified syntax.

for a senior

Reason about left-to-right execution, mixing lifecycles, and when ad-hoc goals are appropriate vs full builds.

for a principal

Govern plugin prefix/group configuration and standardize which goals teams run vs CI phases.

## The two argument types Maven (`mvn`) is a build tool driven by the **build lifecycle** — an ordered, fixed sequence of stages called **phases**. The command line lets you pass either phases or plugin goals, and understanding the difference is foundational. ### Lifecycle phases The default lifecycle has phases in this fixed order (abbreviated): `validate` → `compile` → `test-compile` → `test` → `package` → `verify` → `install` → `deploy`. There are two other lifecycles: **clean** (`pre-clean`, `clean`, `post-clean`) and **site** (`pre-site`, `site`, ...). When you run `mvn package`, Maven runs **every phase from the start of that lifecycle up to and including** `package`. So it validates, compiles, runs tests, then packages. This is why `mvn test` also compiles first — it can't run tests without compiling. Phases by themselves do nothing; work happens because **plugin goals are bound to phases**. For a `jar` packaging project, `compiler:compile` is bound to `compile`, `surefire:test` to `test`, `jar:jar` to `package`, and so on. ### Plugin goals (direct invocation) A **goal** is a single unit of work in a **plugin**. You invoke one directly using the form: ``` mvn <prefix>:<goal> e.g. mvn dependency:tree mvn <groupId>:<artifactId>:<version>:<goal> (fully qualified) ``` Direct goal invocation runs **only that goal** — Maven does NOT run any preceding phases. `mvn compiler:compile` compiles but does not validate or run tests. The short `prefix` (e.g. `dependency`, `help`, `compiler`) is resolved via plugin metadata; standard Apache plugins (`maven-*-plugin`) get a prefix automatically. ### Mixing and ordering Arguments execute **left to right**. `mvn clean package` runs the `clean` phase (deletes `target/`) then builds through `package`. `mvn clean compiler:compile flatten:flatten` would run clean, then a single goal, then another single goal. ## When to use which - **Phases**: normal builds — `mvn verify`, `mvn install`, `mvn clean package`. - **Direct goals**: one-off diagnostics or tasks not in the normal flow — `mvn help:effective-pom`, `mvn dependency:tree`, `mvn versions:display-dependency-updates`. ## Key elements - Lifecycles: default, clean, site - Phase binding configured in `<build><plugins>` via `<executions>` - Plugin prefix resolution (`maven-dependency-plugin` → `dependency`)

  • Does `mvn test` compile your code first?
    Yes. `test` is a later phase, so Maven runs validate, compile, test-compile, then test — all preceding default-lifecycle phases.
  • How does Maven turn the short prefix `dependency` into the real plugin?
    Via plugin prefix resolution: it checks the plugin's `META-INF/maven/plugin.xml` goalPrefix and configured plugin groups in settings.xml; Apache `maven-*-plugin` artifacts get an automatic prefix.

A phase is like 'cook dinner' — it implies all prior steps (shop, prep, cook). A goal is like 'just chop the onions' — one isolated task you can do on its own.

saying these in an interview costs you the question

  • Saying `mvn compiler:compile` also runs tests or earlier phases — direct goals run alone.
  • Claiming `clean` is part of the default lifecycle (it's a separate lifecycle).
  • Thinking phases contain logic themselves rather than being binding points for plugin goals.

context

open as a page

What does it mean for a Maven plugin goal to be 'bound' to a lifecycle phase, and why does running a phase execute plugin goals?

level: juniorimportance: must knowfreq 70%

basics

~20 s

A lifecycle phase (like compile) is just a named step that does nothing by itself. Plugin goals are 'bound' to phases, so running 'mvn compile' actually runs the goals attached to that phase, like compiler:compile.

open as a page

Walk through the key phases of the default lifecycle in order and explain what typically happens at each.

level: juniorimportance: must knowfreq 75%

basics

~20 s

The main default phases run in order: validate, compile, test, package, verify, install, deploy. Validate checks the project, compile builds source, test runs unit tests, package makes the jar/war, verify runs checks, install copies to the local repo, deploy uploads to a remote repo.

open as a page

What are Maven's three built-in build lifecycles, and what is each one responsible for?

level: juniorimportance: must knowfreq 70%

basics

~10 s

Maven has three built-in lifecycles: clean (deletes previous build output), default (builds, tests, packages and deploys the project), and site (generates project documentation/reports).

open as a page

What is the difference between `-DskipTests` and `-Dmaven.test.skip=true`, and how do `-D` properties work on the mvn command line in general?

level: middleimportance: must knowfreq 75%

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.

open as a page

Walk me through binding a plugin goal to a custom phase using an <execution> block. What does each sub-element control?

level: middleimportance: must knowfreq 60%

basics

~10 s

Inside a plugin you add <executions><execution>, give it an <id>, name the <phase> to attach to, list the <goals>, and optionally a <configuration>. That tells Maven to run those goals during that phase.

open as a page

When you run 'mvn package', what exactly executes, and why? Explain the cumulative nature of phases.

level: middleimportance: must knowfreq 65%

basics

~10 s

Invoking a phase runs that phase AND every phase before it in the same lifecycle, in order. So 'mvn package' runs validate, compile, and test before package, executing all goals bound to each.

open as a page

Explain the `-o` (offline) and `-U` (update-snapshots) flags. When would you use each, and how do they interact with SNAPSHOT dependencies?

level: middleimportance: should knowfreq 60%

basics

~10 s

-o runs offline using only the local repository, never reaching the network. -U forces Maven to re-check remote repos for newer SNAPSHOTs (and re-attempt failed downloads) instead of using cached copies.

open as a page

How does the <packaging> element change the default goal-to-phase bindings? Contrast jar, war, pom, and maven-plugin.

level: middleimportance: should knowfreq 55%

basics

~10 s

The <packaging> value selects which plugin goals are bound by default. jar binds jar:jar at package; war binds war:war; pom binds almost nothing (only install/deploy); maven-plugin adds plugin descriptor goals.

open as a page

Describe the clean and site lifecycles, including their phases, and explain why 'mvn clean package' is a common command.

level: middleimportance: should knowfreq 45%

basics

~20 s

The clean lifecycle (pre-clean, clean, post-clean) deletes the target directory. The site lifecycle (pre-site, site, post-site, site-deploy) builds project docs. 'mvn clean package' is common because it deletes old output first, then does a fresh build.

open as a page

When a multi-module Maven build fails, how do `-e`, `-X`, `--fail-at-end`, and `--fail-never` change what happens and what you see?

level: seniorimportance: should knowfreq 50%

basics

~10 s

-e prints full stack traces on errors. -X enables verbose debug logging. --fail-at-end keeps building unaffected modules then reports all failures. --fail-never never fails the overall build regardless of module errors.

open as a page

How do you activate Maven profiles from the command line with `-P`, and how does that relate to other activation mechanisms?

level: seniorimportance: should knowfreq 55%

basics

~10 s

-P profile1,profile2 activates named profiles for that build. Prefix with ! (e.g. -P !prod) to deactivate one. Profiles can also auto-activate via <activation> rules or settings.xml.

open as a page

What is the difference between invoking `mvn test` (a phase) and `mvn surefire:test` (a goal directly)? When would you use each?

level: seniorimportance: should knowfreq 45%

basics

~20 s

mvn test runs the whole lifecycle up to test (validate, compile, test-compile, then tests). mvn surefire:test runs only that one goal, skipping compilation, so it can fail or use stale classes if you haven't built first.

open as a page

Maven has three built-in lifecycles. Name them and explain how 'clean' and 'site' relate to the default lifecycle and its goal bindings.

level: seniorimportance: should knowfreq 35%

basics

~10 s

The three lifecycles are clean, default, and site. They are independent. mvn clean package runs the clean lifecycle then the default lifecycle. clean binds clean:clean; site binds site:site.

open as a page

How do plugin goals get associated with lifecycle phases, and how can you bind your own goal to a specific phase?

level: seniorimportance: should knowfreq 55%

basics

~10 s

Lifecycle phases run plugin goals bound to them. Some bindings are default (chosen by packaging type); you add your own by declaring a plugin <execution> with a <phase> and <goals> in the POM's <build><plugins>.

open as a page

How would you disable or override an inherited goal binding (e.g., skip the default jar or surefire execution) coming from packaging or a parent POM?

level: principalimportance: should knowfreq 30%

basics

~10 s

Redeclare the plugin and its execution by the same id and set <phase>none</phase> to unbind it, or use the plugin's skip flag (e.g., -DskipTests / <skip>true</skip>). For default packaging executions, target the id 'default-<goal>'.

open as a page