TExcelTAD
Home › Java Builds: Maven & Gradle › Lifecycles, Phases & Goals

Lifecycles, Phases & Goals

mvn package doesn't mean "run the package step". It means "run every phase of the lifecycle up to and including package". Understanding that one sentence, and the difference between a phase and a goal, explains most of Maven's behaviour.

Three lifecycles

Maven has three built-in lifecycles, each an ordered list of phases:

Lifecycle Phases (main ones) Typical command
clean pre-clean, clean, post-clean mvn clean
default validate, compile, test-compile, test, package, verify, install, deploy (23 in total) mvn verify
site pre-site, site, post-site, site-deploy mvn site

A phase does nothing by itself. Work is done by goals (also called mojos): compiler:compile, surefire:test, jar:jar. A goal is bound to a phase, and when the build reaches the phase, the bound goals run. The packaging (jar, pom, war) decides the default bindings. help:describe prints them:

bash · try it yourself
mkdir -p lc/src/main/java/demo && cd lc
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>lc</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>
POM
echo 'package demo; public class A {}' > src/main/java/demo/A.java
mvn -o help:describe -Dcmd=deploy | sed -n '/^It is a part/,/^\* deploy/p' | grep -v 'Not defined'
It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes the following phases:
* process-resources: org.apache.maven.plugins:maven-resources-plugin:3.4.0:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:3.15.0:compile
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:3.4.0:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:3.15.0:testCompile
* test: org.apache.maven.plugins:maven-surefire-plugin:3.5.4:test
* package: org.apache.maven.plugins:maven-jar-plugin:3.5.0:jar
* install: org.apache.maven.plugins:maven-install-plugin:3.1.4:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:3.1.4:deploy

Of the 23 phases, only 8 have something bound for a jar project; the empty ones (generate-sources, prepare-package, integration-test, …) are hooks where your plugins can attach work. The plugin versions shown (compiler 3.15.0, surefire 3.5.4) are the defaults built into Maven 3.9.16; a POM should pin its own, as the next lesson shows.

We add -o (offline) whenever we call a plugin by its prefix (help:, dependency:) on this machine: otherwise Maven first tries to refresh the plugin list from Maven Central, fails without a network and prints warnings. On your machine you won't need it.

Phases run everything before them

Watch which goals run for three different commands. Every goal execution prints a header line --- plugin:version:goal (execution-id) @ project ---, so a grep gives the plan:

bash · try it yourself
mkdir -p lc/src/main/java/demo && cd lc
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>lc</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>
POM
echo 'package demo; public class A {}' > src/main/java/demo/A.java
echo '$ mvn compile';            mvn compile            | grep -- '--- '
echo '$ mvn clean package';      mvn clean package      | grep -- '--- '
echo '$ mvn compiler:compile';   mvn compiler:compile   | grep -- '--- '
$ mvn compile
[INFO] --- resources:3.4.0:resources (default-resources) @ lc ---
[INFO] --- compiler:3.15.0:compile (default-compile) @ lc ---
$ mvn clean package
[INFO] --- clean:3.2.0:clean (default-clean) @ lc ---
[INFO] --- resources:3.4.0:resources (default-resources) @ lc ---
[INFO] --- compiler:3.15.0:compile (default-compile) @ lc ---
[INFO] --- resources:3.4.0:testResources (default-testResources) @ lc ---
[INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ lc ---
[INFO] --- surefire:3.5.4:test (default-test) @ lc ---
[INFO] --- jar:3.5.0:jar (default-jar) @ lc ---
$ mvn compiler:compile
[INFO] --- compiler:3.15.0:compile (default-cli) @ lc ---
  • mvn compile ran the two goals bound up to compile.
  • mvn clean package ran clean from the clean lifecycle, then every bound goal of the default lifecycle up to package. You can list several phases and goals; Maven runs them in the order given.
  • mvn compiler:compile runs one goal with no lifecycle at all. Its execution id is default-cli (invoked from the command line) instead of default-compile (bound by the packaging).

That last form is useful for plugins that aren't part of the build, such as dependency:tree, help:effective-pom or versions:display-dependency-updates, but dangerous for build steps: mvn jar:jar packages whatever is in target/classes, compiled or not.

The effective POM

Your pom.xml is merged with its parents and the Super POM, profiles are applied, and default bindings are added. The result, the effective POM, is what Maven actually executes. When a build does something you didn't write, look there:

bash · try it yourself
mkdir -p lc && cd lc
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>lc</artifactId>
  <version>1.0</version>
</project>
POM
mvn -q -o help:effective-pom -Doutput=effective.xml
echo "pom.xml: $(wc -l < pom.xml) lines, effective POM: $(wc -l < effective.xml) lines"
grep -m1 '<url>https://repo' effective.xml
grep -B1 -A10 '<artifactId>maven-compiler-plugin' effective.xml | head -12
pom.xml: 6 lines, effective POM: 234 lines
      <url>https://repo.maven.apache.org/maven2</url>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.15.0</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>

A 6-line POM became more than 200 lines: Maven Central as the repository, the directory layout, and every default plugin with its executions. The execution ids default-compile and default-testCompile are the names you use to reconfigure or disable a default binding.

A model of the lifecycle

The rule is simple enough to write down. This model takes the jar bindings above and computes what a command line runs:

python · try it yourself
DEFAULT = ["validate", "initialize", "generate-sources", "process-sources", "generate-resources",
           "process-resources", "compile", "process-classes", "generate-test-sources",
           "process-test-sources", "generate-test-resources", "process-test-resources",
           "test-compile", "process-test-classes", "test", "prepare-package", "package",
           "pre-integration-test", "integration-test", "post-integration-test", "verify",
           "install", "deploy"]
CLEAN = ["pre-clean", "clean", "post-clean"]
BINDINGS = {"clean": ["clean:clean"], "process-resources": ["resources:resources"],
            "compile": ["compiler:compile"], "process-test-resources": ["resources:testResources"],
            "test-compile": ["compiler:testCompile"], "test": ["surefire:test"],
            "package": ["jar:jar"], "install": ["install:install"], "deploy": ["deploy:deploy"]}

def plan(*args):
    goals = []
    for arg in args:
        lifecycle = next((lc for lc in (CLEAN, DEFAULT) if arg in lc), None)
        if lifecycle is None:                      # not a phase: a goal run on its own
            goals.append(arg)
            continue
        for phase in lifecycle[: lifecycle.index(arg) + 1]:
            goals += BINDINGS.get(phase, [])
    return goals

for cmd in [("compile",), ("clean", "package"), ("verify",), ("compiler:compile",), ("install", "dependency:tree")]:
    print("mvn", " ".join(cmd).ljust(24), "->", ", ".join(plan(*cmd)))
mvn compile                  -> resources:resources, compiler:compile
mvn clean package            -> clean:clean, resources:resources, compiler:compile, resources:testResources, compiler:testCompile, surefire:test, jar:jar
mvn verify                   -> resources:resources, compiler:compile, resources:testResources, compiler:testCompile, surefire:test, jar:jar
mvn compiler:compile         -> compiler:compile
mvn install dependency:tree  -> resources:resources, compiler:compile, resources:testResources, compiler:testCompile, surefire:test, jar:jar, install:install, dependency:tree

Tip: In CI, run mvn verify (or mvn clean verify), not mvn install. verify runs every check, including integration tests, without copying artifacts into the CI machine's local repository, where they could leak into the next build.

Test yourself

What does mvn package execute?

  1. Only the goals bound to package
  2. The goals bound to every phase of the default lifecycle up to and including package
  3. clean, then package
  4. Every goal of every plugin in the POM

What is the difference between mvn compile and mvn compiler:compile?

  1. None
  2. The first is a phase that also runs earlier phases (e.g. resources); the second runs one goal without the lifecycle
  3. The second also runs tests
  4. The first is offline

Where do you see the default plugin bindings and inherited settings that your POM never mentions?

  1. In settings.xml
  2. In the effective POM (mvn help:effective-pom)
  3. In target/classes
  4. In the archetype