Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 41 ยท Lifecycle and Plugins

Lifecycles and Phases

Understand Maven's three lifecycles (default, clean, site), every phase from validate to deploy, and why running one phase also runs all the phases before it.

When you type mvn package, Maven does not have one big "package" program. It walks through an ordered list of named steps, called phases, stops after the one you asked for, and runs whatever work is attached to each phase on the way. That ordered list is the build lifecycle, and it is the central idea of Maven.

This lesson lists the lifecycles and their phases, explains what "running a phase" means, and shows a phase-runner stand-in in Java. The real Maven cannot run on this site, so all mvn commands and logs are read-only; the TryIts are simplified simulators and say so.

Three built-in lifecycles

default Builds and ships your project.
clean Removes the target folder.
site Generates project documentation.

They are independent, so clean does not run as part of default.

1 / 3

Phases are only names

A phase is a named position in the lifecycle. By itself a phase does nothing. Work happens when plugin goals are attached ("bound") to phases. The compile phase compiles code because the compiler plugin's compile goal is bound to it. The package phase builds a JAR because the jar plugin's jar goal is bound to it. Plugins and Goals covers goals in depth.

Two consequences are worth remembering:

  • A phase with nothing bound to it is skipped quickly. Many of the 23 default phases do nothing for a simple JAR project.
  • Maven's behaviour comes from plugin bindings, not from the phase names, which are only a shared vocabulary.

The default lifecycle

The default lifecycle has 23 phases. You will meet a handful every day, but the full list helps you read build logs.

Phase Purpose (simple JAR project)
validate Check the project is correct and information is available.
initialize Set up properties and directories.
generate-sources Generate source code, if any.
process-sources Process sources (for example, filtering).
generate-resources Generate resources, if any.
process-resources Copy src/main/resources into target/classes.
compile Compile src/main/java into target/classes.
process-classes Post-process classes (bytecode tools).
generate-test-sources Generate test sources, if any.
process-test-sources Process test sources.
generate-test-resources Generate test resources.
process-test-resources Copy src/test/resources into target/test-classes.
test-compile Compile src/test/java.
process-test-classes Post-process test classes.
test Run unit tests.
prepare-package Last-minute work before packaging.
package Create the JAR (or WAR, or other format).
pre-integration-test Prepare the environment for integration tests.
integration-test Run integration tests.
post-integration-test Tear the environment down.
verify Check the package meets quality criteria.
install Copy the package into your local repository.
deploy Upload the package to a remote repository.
flowchart LR
    A["validate"] --> B["process-resources"]
    B --> C["compile"]
    C --> D["test-compile"]
    D --> E["test"]
    E --> F["package"]
    F --> G["verify"]
    G --> H["install"]
    H --> I["deploy"]

A selection of the default lifecycle's 23 phases, in the order Maven runs them.

Running a phase runs every earlier phase

This is the rule that explains most Maven behaviour. When you run a phase, Maven runs all phases before it in the same lifecycle, then that phase, then stops.

Terminal
mvn compile    # validate ... process-resources, compile
mvn test       # everything up to and including test
mvn package    # up to package; test runs too
mvn install    # up to install; also copies the JAR to ~/.m2

That is why mvn package runs your unit tests: test comes before package. To skip them you must say so explicitly (-DskipTests, covered in The Command Line Toolbox).

You can list several phases and Maven runs them left to right. The common pair is:

Terminal
mvn clean package

clean belongs to a different lifecycle, so it must be named explicitly. It then runs, followed by the default lifecycle up to package.

The clean and site lifecycles

The clean lifecycle has three phases: pre-clean, clean and post-clean. The clean phase has the clean plugin's clean goal bound to it, which deletes the target directory. Use it when you suspect stale output.

The site lifecycle has pre-site, site, post-site and site-deploy. It generates a project web site with reports. Many teams never use it, since modern documentation tools often replace it, but it is part of the standard.

What the log shows

Every Maven log line of the form --- plugin:version:goal (execution-id) @ project --- tells you which goal is running as part of which phase. A typical mvn package for a small JAR looks like this (versions will differ):

TEXT
[INFO] --- resources:3.3.1:resources (default-resources) @ hello ---
[INFO] --- compiler:3.13.0:compile (default-compile) @ hello ---
[INFO] --- resources:3.3.1:testResources (default-testResources) @ hello ---
[INFO] --- compiler:3.13.0:testCompile (default-testCompile) @ hello ---
[INFO] --- surefire:3.2.5:test (default-test) @ hello ---
[INFO] --- jar:3.4.1:jar (default-jar) @ hello ---
[INFO] BUILD SUCCESS

The order mirrors the lifecycle: resources, compile, test resources, test compile, test, jar. You can read a build log as a trace of the lifecycle.

A phase runner

The simulator below holds the default lifecycle for a JAR project as an ordered list, with the goals that Maven binds by default. It then "runs" a requested phase by walking the list up to and including it. It is a stand-in: real Maven builds the plan from the model, plugin descriptors and your POM, and then calls actual plugin code.

Java Run a phase, run everything before it (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Look at the three runs: compile stops at the compiler goal, package includes surefire:test on the way to jar:jar, and clean uses its own separate list. The final line shows that most phases are only empty slots for a plain JAR, which is why the build is still quick.

Command-line patterns

Goal Command
Build from scratch mvn clean package
Run unit tests mvn test
Run unit and integration tests mvn verify
Put the artifact in your local repository mvn install
Compile only, quickly mvn compile
Check the POM is valid mvn validate

Choose the earliest phase that gives you what you need. Running mvn install just to see if code compiles wastes time on tests and packaging.

A second simulator: counting the work

To see why picking the right phase matters, this stand-in assigns a made-up cost in milliseconds to each bound goal and totals the work for different commands. The numbers are invented for illustration; real timings depend on your project.

Java What does each command cost? (stand-in with invented numbers)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The test phase dominates in this model, which is typical. It also explains why teams use -DskipTests or mvn compile for quick local loops, and run the full lifecycle in CI.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You run mvn package. Which of these also happens?

    Running a phase runs every earlier phase of the same lifecycle. compile and test come before package, so both run. clean is a separate lifecycle and install comes after package.
  2. 2. How many built-in lifecycles does Maven have?

    The built-in lifecycles are default (build), clean (remove output) and site (generate documentation).
  3. 3. What does a phase such as compile do on its own?

    A phase is a name for a point in the lifecycle. The compiler plugin's compile goal is bound to it by default, and that goal does the compiling.
  4. 4. Which command runs unit tests AND integration tests, stopping before installation?

    verify comes after integration-test and before install. mvn test stops earlier and never reaches integration tests.

Summary

  • Maven has three lifecycles: default, clean and site; each is an ordered list of phases.
  • The default lifecycle runs from validate through compile, test, package, verify and install to deploy.
  • Running a phase runs all earlier phases of its lifecycle; name clean separately.
  • Phases do nothing by themselves; plugin goals bound to them do the work.
  • Log lines of the form plugin:version:goal (execution) @ project trace the lifecycle.
  • The runners above are stand-ins that model the idea; the real build is performed by Maven and its plugins.

Next, look at the work itself: Plugins and Goals.