Sub topic 6
Maven Lifecycles, Phases, and Goals
In the last chapter you wrote a pom.xml. That file describes your project, but it never says "first compile, then test, then make a JAR." Maven already knows that order. It is built into a set of fixed steps called the lifecycle. This chapter explains the lifecycle, what happens at each step, and how to choose which steps to run.
Three Words to Know
Maven uses three related terms. Learn them first, because the rest of the chapter depends on them.
- A lifecycle is a named, ordered list of steps for one kind of job, such as building or cleaning.
- A phase is one step in a lifecycle, such as
compileortest. A phase is only a name. On its own it does no work. - A goal is a single task that a plugin performs, such as compiling Java code. A plugin is a piece of software that Maven downloads to do real work. Goals are written as
plugin:goal, for examplecompiler:compile.
Goals attach to phases. When Maven reaches a phase, it runs every goal attached to it. This attachment is called a binding.
The Three Built-In Lifecycles
Maven has exactly three lifecycles.
| Lifecycle | What it is for | Main phases |
|---|---|---|
| `clean` | Deletes earlier build output | `pre-clean`, `clean`, `post-clean` |
| `default` | Builds, tests, and ships your project | `validate`, `compile`, `test`, `package`, `verify`, `install`, `deploy` |
| `site` | Makes a documentation website | `pre-site`, `site`, `post-site`, `site-deploy` |
You will use default most of the time. You will use clean often. Many projects never use site.
The Default Lifecycle Step by Step
The default lifecycle has over twenty phases. Most are small in-between steps. These are the ones you will type:
validatechecks that the POM is correct and has everything it needs.compileturns the code insrc/main/javainto.classfiles intarget/classes.testcompiles the code insrc/test/javaand runs your unit tests.packagebundles the compiled classes into a file such as a JAR.verifyruns any extra checks, such as integration tests or quality rules.installcopies the package into your local repository at~/.m2/repository, so other projects on your machine can use it.deployuploads the package to a shared remote repository so other people can use it.
The Key Rule: Earlier Phases Always Run
When you ask Maven to run a phase, it runs every earlier phase in that lifecycle first, in order. Then it stops.
So mvn package does not only package. It validates, compiles, tests, and then packages. This rule keeps you safe. You cannot build a JAR from code that was never compiled, and you cannot skip the tests by accident.
Which Goals Are Bound to Which Phases
Your POM did not list a compiler plugin or a JAR plugin, yet Maven compiles and packages. This works because the <packaging> element picks a set of default bindings. For jar packaging, the bindings are:
| Phase | Goal bound to it | What the goal does |
|---|---|---|
| `process-resources` | `resources:resources` | Copies `src/main/resources` into `target/classes` |
| `compile` | `compiler:compile` | Compiles main code |
| `process-test-resources` | `resources:testResources` | Copies test resources |
| `test-compile` | `compiler:testCompile` | Compiles test code |
| `test` | `surefire:test` | Runs unit tests |
| `package` | `jar:jar` | Builds the JAR file |
| `install` | `install:install` | Copies the JAR to `~/.m2/repository` |
| `deploy` | `deploy:deploy` | Uploads the JAR to a remote repository |
Phases such as validate and verify have no default goals for a plain JAR project. Maven passes through them and does nothing. Later, plugins can add goals to them.
Worked Example: Running the Phases
Use the hello project from the last chapter. It contains App.java, AppTest.java, and the pom.xml. Run these commands one at a time from the folder that holds pom.xml:
mvn compile
mvn test
mvn package
java -cp target/hello-1.0-SNAPSHOT.jar com.example.App
mvn dependency:tree
mvn clean verifyHere is what each line does.
mvn compile runs validate and then compile, including small steps such as process-resources. The output contains lines like this:
[INFO] --- resources:3.3.1:resources (default-resources) @ hello ---
[INFO] --- compiler:3.13.0:compile (default-compile) @ hello ---
[INFO] BUILD SUCCESSEach --- line names a goal, its plugin version, and the project. Your version numbers may differ. Reading these lines tells you exactly which goals ran. After this step, target/classes/com/example/App.class exists.
mvn test runs everything up to test. It compiles the test class and Surefire runs it. You should see Tests run: 1, Failures: 0.
mvn package goes one step further and writes target/hello-1.0-SNAPSHOT.jar. The name comes from your artifactId and version.
java -cp ... runs your program from the new JAR. It prints Hello, World!. Here you name the main class on the command line because the default JAR has no Main-Class entry. So java -jar would fail on this JAR. A later chapter on plugins shows how to add that entry.
mvn dependency:tree is a goal, not a phase. Notice the colon. When you name a goal directly, Maven runs only that goal. No earlier phases run. This goal prints the project's dependencies as a tree.
mvn clean verify names two phases from two lifecycles. Maven runs them left to right. First the clean lifecycle deletes the target folder. Then the default lifecycle runs from validate through verify. This gives you a fresh, full build. Many teams use it before they push code.
Phases Versus Goals on the Command Line
| You type | Maven treats it as | What runs |
|---|---|---|
| `mvn package` | A phase | All phases up to `package` and their goals |
| `mvn compiler:compile` | A goal | Only that one goal |
| `mvn clean package` | Two phases | `clean` lifecycle, then default up to `package` |
| `mvn clean dependency:tree` | A phase and a goal | `clean`, then the single goal |
Common Mistakes
Running mvn install out of habit. install writes to your local repository. You only need it when another project on your machine depends on this one. For a normal check, mvn verify does the same work without touching ~/.m2.
Running clean every time. clean throws away all compiled output, so the next build starts from zero. It is useful when results look stale or wrong. For everyday work, mvn test alone is faster.
Mixing up the two skip flags.
| Flag | Effect |
|---|---|
| `-DskipTests` | Compiles the tests but does not run them |
| `-Dmaven.test.skip=true` | Neither compiles nor runs the tests |
Both flags hide failing tests, so use them rarely. The second flag can also break builds whose other modules need the test classes.
Expecting deploy to work right away. deploy needs a remote repository set up in the POM and login details. Without them it fails at the last step, after everything else has already succeeded.
Summary
- A lifecycle is an ordered list of phases, and Maven has three of them:
clean,default, andsite. - A phase is a named step. Goals do the real work, and they are bound to phases.
- Running a phase runs all earlier phases in the same lifecycle.
- Running a goal such as
dependency:treeruns only that goal. - Your
<packaging>value decides the default goal bindings.
Exercise
You run mvn package on a fresh project. List, in order, the main default-lifecycle phases that run, and name the goal that creates the JAR.
Show answer
validate, compile, test, package (with small steps such as process-resources, test-compile, and process-test-resources in between). The goal jar:jar, bound to the package phase, creates the JAR.
Exercise
What is the difference between mvn test and mvn surefire:test?
Show answer
mvn test is a phase, so Maven first runs every earlier phase, including compiling the main and test code, and then runs the tests. mvn surefire:test is a single goal, so only the test runner runs. If the code was never compiled, it has nothing to test.
Exercise
A teammate types mvn clean install before every commit, but no other local project uses this one. Suggest a better command and explain why.
Show answer
Use mvn clean verify. It runs the same compile, test, package, and verify steps but stops before install, so it does not copy the JAR into ~/.m2/repository when nothing needs it there.
Exercise
Why does java -jar target/hello-1.0-SNAPSHOT.jar fail while java -cp target/hello-1.0-SNAPSHOT.jar com.example.App works?
Show answer
The default jar:jar goal does not write a Main-Class entry into the JAR's manifest, so java -jar does not know which class to start. With -cp, you name the main class yourself.