The build lifecycle: compile, test, package
40 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)You type mvn package and wonder what really ran, or why the JAR never appeared. Maven builds in a fixed order, and knowing that order explains every success and every failure.
Phases in order: validate, compile, test, package, verify, install
The build lifecycle is Maven's fixed, ordered list of build steps, called phases: validate, compile, test, package, verify and install. You name the phase you want, and Maven runs the list up to it.
Ask Maven to describe the package command. It prints every phase of the lifecycle, in order, and the goal attached to each one.
Example 1 · mvn help:describe: every phase and its goal
Runs in: a terminal in the receipt-app Maven project
mvn help:describe "-Dcmd=package"$ mvn help:describe "-Dcmd=package" [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- help:3.5.2:describe (default-cli) @ receipt-app --- [INFO] 'package' is a phase corresponding to this plugin: org.apache.maven.plugins:maven-jar-plugin:3.5.0:jar It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes the following phases: * validate: Not defined * initialize: Not defined * generate-sources: Not defined * process-sources: Not defined * generate-resources: Not defined * 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-classes: Not defined * generate-test-sources: Not defined * process-test-sources: Not defined * generate-test-resources: Not defined * 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 * process-test-classes: Not defined * test: org.apache.maven.plugins:maven-surefire-plugin:3.5.4:test * prepare-package: Not defined * package: org.apache.maven.plugins:maven-jar-plugin:3.5.0:jar * pre-integration-test: Not defined * integration-test: Not defined * post-integration-test: Not defined * verify: Not defined * install: org.apache.maven.plugins:maven-install-plugin:3.1.4:install * deploy: org.apache.maven.plugins:maven-deploy-plugin:3.1.4:deploy [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------
What this code does
- Phases marked "Not defined" have no goal attached for
jarpackaging, so they cost nothing. Eight phases have a goal attached; six of them run on the way topackage. - Each attached goal is shown as
groupId:artifactId:version:goal, for example the compiler plugin'scompilegoal on thecompilephase.
A phase is one named step. validate checks the pom, compile compiles src/main/java, test compiles and runs the tests in src/test/java, package builds the JAR in target, verify runs extra checks such as integration tests, and install copies the JAR into your local repository.
The full lifecycle has smaller phases between these, such as process-resources and test-compile, and many do nothing in a simple project. The order never changes, so you can clone a stranger's project and type mvn package with confidence.
Order of execution
Put the phases in order
Arrange the phases the way Maven runs them.
deploy, the phase after install, uploads the JAR to a shared repository such as a company's Nexus server. Students rarely need it.Running a phase runs every phase before it
A phase is one named step of the Maven lifecycle, such as compile or test. Naming a phase runs every earlier phase first, in order.
mvn package does not only package. Compare the goal lines that compile, test and package -DskipTests print, and see how each command repeats the earlier steps.
Example 2 · compile, test, package -DskipTests: which goals run
Runs in: a terminal in the receipt-app Maven project
mvn compilemvn testmvn package -DskipTests$ mvn compile [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] BUILD SUCCESS $ mvn test [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] --- resources:3.4.0:testResources (default-testResources) @ receipt-app --- [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ receipt-app --- [INFO] --- surefire:3.5.4:test (default-test) @ receipt-app --- [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.138 s -- in com.college.app.FeeCalculatorTest [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] BUILD SUCCESS $ mvn package -DskipTests [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] --- resources:3.4.0:testResources (default-testResources) @ receipt-app --- [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ receipt-app --- [INFO] --- surefire:3.5.4:test (default-test) @ receipt-app --- [INFO] Tests are skipped. [INFO] --- jar:3.5.0:jar (default-jar) @ receipt-app --- [INFO] BUILD SUCCESS
What this code does
- Each command runs every phase before the one you named:
testrepeats resources and compile first,packagerepeats all of them. - With
-DskipTeststhe surefire goal still runs but says "Tests are skipped.", and the JAR is built.
Example 3 · mvn package with passing tests
Runs in: a terminal in the receipt-app Maven project (with FeeCalculator and FeeCalculatorTest)
mvn package$ mvn package [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] skip non existing resourceDirectory C:\Users\asha\projects\receipt-app\src\main\resources [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] Recompiling the module because of changed source code. [INFO] Compiling 2 source files with javac [debug release 17] to target\classes [INFO] --- resources:3.4.0:testResources (default-testResources) @ receipt-app --- [INFO] skip non existing resourceDirectory C:\Users\asha\projects\receipt-app\src\test\resources [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ receipt-app --- [INFO] Recompiling the module because of changed dependency. [INFO] Compiling 1 source file with javac [debug release 17] to target\test-classes [INFO] --- surefire:3.5.4:test (default-test) @ receipt-app --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.college.app.FeeCalculatorTest [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.124 s -- in com.college.app.FeeCalculatorTest [INFO] Results: [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] --- jar:3.5.0:jar (default-jar) @ receipt-app --- [INFO] Building jar: C:\Users\asha\projects\receipt-app\target\receipt-app-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.723 s [INFO] Finished at: 2026-10-16T10:19:41+05:30 [INFO] ------------------------------------------------------------------------
What this code does
- Follow the
---lines: resources, compile, testResources, testCompile, test, jar. The JAR is built only after "Tests run: 3, Failures: 0". - "Recompiling the module because of changed source code" shows Maven checking what changed; an unchanged project compiles faster.
This is a safety rule: a JAR is built only from code that compiled and passed its tests, because those phases come first. If any phase fails, Maven stops at once and later phases do not run.
-DskipTests compiles the test classes but does not run them. Use it only for a quick local JAR, never to hide a failing test.
Predict the output
Which phases run?
Naming a phase runs everything before it. Pick the right answer.
Round 1 of 3
Which phases does this command run?
mvn clean and what lives in target
mvn clean deletes the target folder, so the next build starts from nothing. It belongs to a separate, small lifecycle, so it runs only when you ask for it.
List target, clean it, then install the JAR. You can combine commands: mvn clean package deletes target first and then builds from nothing.
Example 4 · mvn clean and mvn install
Runs in: a terminal in the receipt-app Maven project
ls targetmvn cleanlsmvn install$ ls target classes generated-sources generated-test-sources maven-archiver maven-status receipt-app-1.0-SNAPSHOT.jar surefire-reports test-classes $ mvn clean [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- clean:3.2.0:clean (default-clean) @ receipt-app --- [INFO] Deleting C:\Users\asha\projects\receipt-app\target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.283 s [INFO] Finished at: 2026-10-16T10:14:18+05:30 [INFO] ------------------------------------------------------------------------ $ ls pom.xml src $ mvn install ... [INFO] --- jar:3.5.0:jar (default-jar) @ receipt-app --- [INFO] Building jar: C:\Users\asha\projects\receipt-app\target\receipt-app-1.0-SNAPSHOT.jar [INFO] --- install:3.1.4:install (default-install) @ receipt-app --- [INFO] Installing C:\Users\asha\projects\receipt-app\pom.xml to C:\Users\asha\.m2\repository\com\college\receipt-app\1.0-SNAPSHOT\receipt-app-1.0-SNAPSHOT.pom [INFO] Installing C:\Users\asha\projects\receipt-app\target\receipt-app-1.0-SNAPSHOT.jar to C:\Users\asha\.m2\repository\com\college\receipt-app\1.0-SNAPSHOT\receipt-app-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
What this code does
- Before cleaning,
targetholds compiled classes, test classes, test reports and the JAR.mvn cleandeletes the whole folder; onlypom.xmlandsrcremain. mvn installruns the full build and then copies the pom and JAR into.m2/repository/com/college/receipt-app/1.0-SNAPSHOT, so another project could depend oncom.college:receipt-app:1.0-SNAPSHOT.
target holds everything the build made: classes, test-classes, surefire-reports (test results), maven-status (notes for faster rebuilds) and the JAR. Maven reuses what it can and never deletes old files on its own, so a renamed or deleted class can leave a stale .class file behind.
Run mvn clean package before you share a JAR or when a build behaves strangely. mvn install then copies the JAR into ~/.m2/repository, where other projects on the same computer can use it as a dependency.
Plugins and goals: the compiler and surefire plugins behind the phases
A plugin is a JAR that does build work. It offers one or more goals, and each goal is one task, written plugin:goal, such as compiler:compile.
Phases are only names; plugins do the work. This pom pins the compiler and surefire plugin versions, so every computer builds in the same way.
Example 5 · pom.xml with pinned compiler and surefire versions
Runs in: the receipt-app Maven project
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.college</groupId> <artifactId>receipt-app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.release>17</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.15.0</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>26.7.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.14.4</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.15.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.5.6</version> </plugin> </plugins> </build></project>$ mvn test [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] --- resources:3.4.0:testResources (default-testResources) @ receipt-app --- [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ receipt-app --- [INFO] --- surefire:3.5.6:test (default-test) @ receipt-app --- [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] BUILD SUCCESS
What this code does
<build><plugins>names each plugin with an exact version. The log now showssurefire:3.5.6instead of the 3.5.4 that this Maven installation would pick by itself.- Pinned versions make the build the same for every teammate and build server, whatever Maven 3.9 release they have installed.
A goal is one task of a plugin: compiler:compile, surefire:test, jar:jar. The packaging type (jar) decides which goal is attached to which phase. The log line --- surefire:3.5.4:test (default-test) reads as plugin surefire, version 3.5.4, goal test.
A goal on its own runs only that goal, so mvn surefire:test on a cleaned project finds nothing to test. Without versions in your pom, plugin versions come from your Maven installation, so two people can get different plugins; the Maven Wrapper, a later lesson, pins Maven itself.
A failing JUnit test stops the build
Surefire is the Maven plugin that runs your unit tests in the test phase. It prints every failure and saves one report per test class in target/surefire-reports.
A bug in FeeCalculator makes one test fail. Watch the build stop with BUILD FAILURE, then read the report from the terminal.
Example 6 · A failing test stops the build, and the surefire report
Runs in: a terminal in the receipt-app Maven project
mvn packagecat target/surefire-reports/com.college.app.FeeCalculatorTest.txtFeeCalculator now has a bug: int discount = (tuition + hostel) * scholarshipPercent / 100; $ mvn package [INFO] Scanning for projects... [INFO] ----------------------< com.college:receipt-app >----------------------- [INFO] Building receipt-app 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] --- resources:3.4.0:resources (default-resources) @ receipt-app --- [INFO] skip non existing resourceDirectory C:\Users\asha\projects\receipt-app\src\main\resources [INFO] --- compiler:3.15.0:compile (default-compile) @ receipt-app --- [INFO] Recompiling the module because of changed source code. [INFO] Compiling 2 source files with javac [debug release 17] to target\classes [INFO] --- resources:3.4.0:testResources (default-testResources) @ receipt-app --- [INFO] skip non existing resourceDirectory C:\Users\asha\projects\receipt-app\src\test\resources [INFO] --- compiler:3.15.0:testCompile (default-testCompile) @ receipt-app --- [INFO] Recompiling the module because of changed dependency. [INFO] Compiling 1 source file with javac [debug release 17] to target\test-classes [INFO] --- surefire:3.5.4:test (default-test) @ receipt-app --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.college.app.FeeCalculatorTest [ERROR] Tests run: 3, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.150 s <<< FAILURE! -- in com.college.app.FeeCalculatorTest [ERROR] com.college.app.FeeCalculatorTest.scholarshipReducesTuitionOnly -- Time elapsed: 0.018 s <<< FAILURE! org.opentest4j.AssertionFailedError: expected: <110000> but was: <101600> at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151) at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132) at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145) at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:531) at com.college.app.FeeCalculatorTest.scholarshipReducesTuitionOnly(FeeCalculatorTest.java:19) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) [INFO] Results: [ERROR] Failures: [ERROR] FeeCalculatorTest.scholarshipReducesTuitionOnly:19 expected: <110000> but was: <101600> [ERROR] Tests run: 3, Failures: 1, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.5.4:test (default-test) on project receipt-app: There are test failures. [ERROR] See C:\Users\asha\projects\receipt-app\target\surefire-reports for the individual test results. [ERROR] See dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. $ cat target/surefire-reports/com.college.app.FeeCalculatorTest.txt ------------------------------------------------------------------------------- Test set: com.college.app.FeeCalculatorTest ------------------------------------------------------------------------------- Tests run: 3, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.150 s <<< FAILURE! -- in com.college.app.FeeCalculatorTest com.college.app.FeeCalculatorTest.scholarshipReducesTuitionOnly -- Time elapsed: 0.018 s <<< FAILURE! org.opentest4j.AssertionFailedError: expected: <110000> but was: <101600> at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151) at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132) at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145) at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:531) at com.college.app.FeeCalculatorTest.scholarshipReducesTuitionOnly(FeeCalculatorTest.java:19) at java.base/java.lang.reflect.Method.invoke(Method.java:580) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) at java.base/java.util.ArrayList.forEach(ArrayList.java:1596) $ ls target classes generated-sources generated-test-sources maven-status surefire-reports test-classes
What this code does
- The test expected 110000 (20% off the tuition only) but got 101600 (20% off tuition and hostel). The stack trace ends at
FeeCalculatorTest.java:19, the failingassertEquals. ls targetshows no JAR: the build stopped in thetestphase. Fixing the discount line back totuition * scholarshipPercent / 100makesmvn packagepass.
Read the failure from the top: the test class and method, the message (expected: <110000> but was: <101600>) and the line of the test (FeeCalculatorTest.java:19). The same details sit in target/surefire-reports, one .txt and one .xml file per test class, and IDEs and build servers read those files.
A compile error stops the build even earlier, in the compile phase, and Maven prints the file, [line,column] and the javac message. Fix the first error first, because later ones are often caused by it.
Next step
Check your understanding
12 quick questions