Lifecycles and phases

Maven has three lifecycles, and each one is a fixed list of phases. A command names a phase, and Maven runs every phase of that lifecycle up to it, in order, and at each phase the plugins that are bound to it. Nothing is skipped and nothing runs out of order, which is why mvn package runs the tests and mvn test does not write a jar. This article reads the lists from the tool itself and follows them through two real builds.

The project

One project is enough for the whole article: a pom, one class and one test class. Every command below runs in its folder, and every log is the real output of that folder.

pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>calc</artifactId> <version>1.0.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.release>21</maven.compiler.release> <junit.version>5.14.3</junit.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </project>

src/main/java/com/example/calc/Calc.java

package com.example.calc; public class Calc { public static int add(int a, int b) { return a + b; } public static void main(String[] args) { System.out.println("1 + 2 = " + add(1, 2)); } }

src/test/java/com/example/calc/CalcTest.java

package com.example.calc; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class CalcTest { @Test void addsTwoNumbers() { assertEquals(4, Calc.add(1, 3)); } }

Three lifecycles

-X turns on the debug log, and Maven prints the three lifecycles it knows before it plans anything. This is the real output of mvn -B -X package on the calculator project, with the lines between them left out:

mvn -B -X package > debug.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" debug.log

[DEBUG] Lifecycle clean -> [pre-clean, clean, post-clean]
[DEBUG] Lifecycle 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]
[DEBUG] Lifecycle site -> [pre-site, site, post-site, site-deploy]
[DEBUG] === PROJECT BUILD PLAN ================================================
[DEBUG] Goal: org.apache.maven.plugins:maven-resources-plugin:3.4.0:resources (default-resources)
[DEBUG] Goal: org.apache.maven.plugins:maven-compiler-plugin:3.15.0:compile (default-compile)
[DEBUG] Goal: org.apache.maven.plugins:maven-resources-plugin:3.4.0:testResources (default-testResources)
[DEBUG] Goal: org.apache.maven.plugins:maven-compiler-plugin:3.15.0:testCompile (default-testCompile)
[DEBUG] Goal: org.apache.maven.plugins:maven-surefire-plugin:3.5.4:test (default-test)
[DEBUG] Goal: org.apache.maven.plugins:maven-jar-plugin:3.5.0:jar (default-jar)

The three lines are the whole model of the build. The clean lifecycle has three phases, the default lifecycle has twenty-three and the site lifecycle has four. The debug log prints them several times because Maven builds the execution plan for each project of a reactor; this project is one, so the lines repeat.

The last part of the excerpt is the build plan: the goals Maven will run for package, in the order it will run them. Six goals for twenty-three phases, because most phases bind nothing for the jar packaging.

The phases of the default lifecycle

In order, with the goal each one runs in a jar project. The versions are the ones of Maven 3.9.16 in this image, from the build plan above and from the log of the package build:

The empty phases are not useless: they are the places a plugin can bind to. A code generator that runs before the compiler binds to generate-sources; an integration test binds to integration-test and its check to verify. The plugins article binds a goal to a phase of its own.

The goal of each phase

A phase is a step of a lifecycle, a goal is a task of a plugin, and the two are joined by an execution: an id, a phase and one or more goals. The jar packaging brings six executions, and each of them is printed in the log as plugin:version:goal (execution-id) @ project. The names in the parentheses of the log are the ids above: default-resources, default-compile, default-test and so on.

The phase list is the contract, the executions are the defaults, and a pom changes the second without touching the first. That is also why a packaging other than jar runs a different set of goals for the same phases: a pom packaging binds none of them, and the parent pom of the multi-module article gets a build with no compile step at all.

clean

The clean lifecycle deletes what the build wrote, and it is separate: a command that names a phase of the default lifecycle never runs it, and mvn clean package runs two lifecycles, one after the other.

mvn -B clean > clean.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" clean.log

status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ calc ---
[INFO] Deleting <project>/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.489 s
[INFO] Finished at: 2026-11-22T08:39:16Z
[INFO] ------------------------------------------------------------------------

clean:3.2.0:clean is the only execution of the lifecycle. The plugin prints one line: the folder it deleted. Run on a project that has not been built yet it prints that line not at all, and the build is still a success, because deleting nothing is not an error. A project whose pom pins another version of the clean plugin prints that version instead, and the archetype article shows the case where the pinned version is not in the local repository at all.

mvn -B package > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log

status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.4.0:resources (default-resources) @ calc ---
[INFO] skip non existing resourceDirectory <project>/src/main/resources
[INFO]
[INFO] --- compiler:3.15.0:compile (default-compile) @ calc ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 1 source file with javac [debug release 21] to target/classes
[INFO]
[INFO] --- resources:3.4.0:testResources (default-testResources) @ calc ---
[INFO] skip non existing resourceDirectory <project>/src/test/resources
[INFO]
[INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ calc ---
[INFO] Recompiling the module because of changed dependency.
[INFO] Compiling 1 source file with javac [debug release 21] to target/test-classes
[INFO]
[INFO] --- surefire:3.5.4:test (default-test) @ calc ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.calc.CalcTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.084 s -- in com.example.calc.CalcTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jar:3.5.0:jar (default-jar) @ calc ---
[INFO] Building jar: <project>/target/calc-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.801 s
[INFO] Finished at: 2026-11-22T08:38:37Z
[INFO] ------------------------------------------------------------------------

Five goals ran and the sixth line is missing because the sixth goal has no output of its own: the jar plugin printed one line and the build ended. Count the goals against the phase list: resources, compile, testResources, testCompile, test, jar. The order is the order of the list, and mvn test would have stopped after the fifth.

The site lifecycle

The site lifecycle has four phases: pre-site, site, post-site and site-deploy. For a jar project the site plugin binds site:3.12.1:site to the second and site:3.12.1:deploy to the fourth, and the first run also needs the project reports plugin. The site plugin is in the local repository of this image; its dependencies are not, so the phase stops at once:

mvn -B site > site.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" site.log

[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:calc >--------------------------
[INFO] Building calc 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- site:3.12.1:site (default-site) @ calc ---
[WARNING] The POM for org.apache.maven:maven-archiver:jar:3.5.2 is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.922 s
[INFO] Finished at: 2026-11-22T08:39:42Z
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.12.1:site (default-site) on project calc: Execution default-site of goal org.apache.maven.plugins:maven-site-plugin:3.12.1:site failed: Plugin org.apache.maven.plugins:maven-site-plugin:3.12.1 or one of its dependencies could not be resolved:
[ERROR] Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact org.apache.maven.reporting:maven-reporting-exec:jar:1.6.0 has not been downloaded from it before.
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

The excerpt keeps the first lines, one of the warnings and the closing error. The full log is 72 lines, mostly one Cannot access central ... in offline mode per missing dependency of the site plugin: the doxia modules, the jetty server it uses to serve the site, the archiver. None of them is in the seed, so nothing can be downloaded and the phase cannot run. A machine with a network runs mvn site and writes the site into target/site.

Where to go next

The next four articles take one phase each: compile, test, package and install, and the switches that change what runs.

Article author: Arthur Isaev