Quick revision
The short answer
A lifecycle is an ordered list of phases, and Maven has three built-in ones: clean, default and site. A phase is a step in that list, and invoking a phase runs every earlier phase of the same lifecycle first. A goal is a single task of a plugin, such as compiler:compile, and goals do the actual work by being bound to phases, with the packaging type deciding the default bindings. So mvn package on a jar project runs validate, compile, test-compile, test and package, among others, which means the tests run unless we skip them.
What to remember
In this answer, we saw that a lifecycle is an ordered list of phases, that Maven has three built-in lifecycles, clean, default and site, and that invoking a phase runs every earlier phase first. Goals are plugin tasks that do the real work, bound to phases by the packaging type or by our POM. On a jar project, mvn package copies resources, compiles main and test code, runs the tests with Surefire and builds the jar, and goals such as dependency:tree can also run on their own without any phase.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A lifecycle is an ordered list of phases, and Maven has three built-in ones: clean, default and site. A phase is a step in that list, and invoking a phase runs every earlier phase of the same lifecycle first. A goal is a single task of a plugin, such as compiler:compile, and goals do the actual work by being bound to phases, with the packaging type deciding the default bindings. So mvn package on a jar project runs validate, compile, test-compile, test and package, among others, which means the tests run unless we skip them.
Why this question is asked
Almost every Java developer types mvn package or mvn install daily, but many cannot say exactly what runs, which is why builds get slow, tests get skipped by accident, or a plugin does not run when expected. Interviewers ask this question to check that we understand the model behind Maven, because every other topic, from Failsafe to plugin configuration, builds on it. In this answer, we will define the three terms with a simple picture, follow what mvn package does on a jar project, see how goals can run on their own, and answer two common follow-ups about clean install and skipping tests.
1Three Words, Three Levels
Maven separates what should happen from how it happens. A lifecycle describes what: an ordered sequence of steps for one purpose. Maven has three built-in lifecycles. The clean lifecycle removes build output. The default lifecycle builds, tests, packages, installs and deploys the project. The site lifecycle generates project documentation.
Each step of a lifecycle is a phase, such as compile, test or package. A phase by itself does nothing; it is a named point in the sequence. When we ask Maven to run a phase, it runs every phase before it in the same lifecycle, in order, and then that phase.
The work is done by goals. A goal is one task provided by a plugin, written plugin:goal, for example compiler:compile or surefire:test. Goals are bound to phases. When Maven reaches a phase, it runs every goal bound to it. Which goals are bound by default depends on the packaging of the project, set by the packaging element in the POM: a jar project gets jar:jar in the package phase, a war project gets war:war instead.
- Lifecycle: defaultan ordered list of phases for building a project
- Phase: testa named step; running it runs every earlier phase first
- Goal: surefire:testa plugin task bound to the phase; it does the actual work
- Plugin: maven-surefire-pluginthe artifact that provides the goal
4 layers, from the top down. Step through them one at a time.
The packaging type decides which goals are bound to which phases by default.
2What mvn package Runs on a Jar Project
The default lifecycle has more than twenty phases. For a jar project, only some of them have goals bound by default. Running mvn package walks through all phases up to package, but only the bound ones do anything visible.
| Phase | Goal bound by default |
|---|---|
| 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 |
The plugin versions in this log are examples and depend on the Maven version and the POM. What matters is the order: copy main resources, compile main code, copy test resources, compile tests, run the tests with Surefire, and finally build the jar. Phases such as validate, initialize, generate-sources and prepare-package also run, but nothing is bound to them by default, so they are silent.
Notice that the test phase comes before package. So mvn package always runs the unit tests unless we skip them, and a failing test stops the build before the jar is created. Each of these log lines also shows the execution id, such as default-compile; those ids are what we use to change the configuration of a default binding.
3Running Goals Directly
We can also call a goal without running any phase. mvn dependency:tree runs only the tree goal of the dependency plugin; it does not compile or test anything. This is how we use utility plugins: dependency:tree, help:effective-pom, versions:display-dependency-updates.
We can mix both in one command. mvn clean package runs the clean lifecycle up to its clean phase, then the default lifecycle up to package. mvn clean dependency:tree runs the clean phase and then the single goal. Maven executes the arguments from left to right.
4Binding Our Own Goals
When we add a plugin to the POM, we decide which phase its goal joins. For example, to check code coverage after the tests, we bind JaCoCo's report goal to the verify phase. From then on, any command that reaches verify, such as mvn verify or mvn install, runs it automatically.
Many goals declare a default phase, which is why the first execution needs no phase element. Writing the phase explicitly makes the build easier to read.
5Follow-up questions
Why is mvn clean install often unnecessary in CI?
A CI job usually starts from a fresh checkout, so there is no target folder to clean; clean costs time and does nothing. install copies the artifacts into the local repository on the build machine, which only helps if a later, separate build on the same machine needs them. Within one multi-module build, modules already see each other through the reactor. So mvn verify is usually the right CI command: it compiles, runs unit and integration tests and all checks bound to verify, without installing. Publishing is done by deploy in the release or main pipeline.
What is the difference between -DskipTests and -Dmaven.test.skip=true?
-DskipTests compiles the test code but does not run it; Surefire and Failsafe skip execution. -Dmaven.test.skip=true skips compiling the tests as well, and is honoured by the compiler plugin and the test plugins. The second is faster but riskier: test code that no longer compiles goes unnoticed, and a module that publishes a test-jar for other modules will not produce it, which can break the build of modules that depend on it.
What happens if we run mvn compile twice?
The compiler plugin checks which sources are newer than their class files, so the second run compiles little or nothing. Incremental detection is not perfect, which is one reason people run clean before a release build.
6Conclusion
In this answer, we saw that a lifecycle is an ordered list of phases, that Maven has three built-in lifecycles, clean, default and site, and that invoking a phase runs every earlier phase first. Goals are plugin tasks that do the real work, bound to phases by the packaging type or by our POM. On a jar project, mvn package copies resources, compiles main and test code, runs the tests with Surefire and builds the jar, and goals such as dependency:tree can also run on their own without any phase.
Open with
A lifecycle is an ordered list of phases, and Maven has three built in: clean, default and site. A phase is a named step in that list, like compile, test or package, and when I invoke a phase Maven runs every earlier phase of the same lifecycle first.
Then, if they want more
Phases themselves do nothing; the work is done by goals, which are tasks provided by plugins, like compiler:compile or surefire:test, and they are bound to phases. The packaging type decides the default bindings, so a jar project binds jar:jar to package and a war project binds war:war.
When I run mvn package on a jar project, Maven processes resources, compiles the main code, processes test resources, compiles the tests, runs them with Surefire in the test phase and then builds the jar. So tests run unless I skip them, and a failing test stops the build before packaging.
I can also run a goal directly, like mvn dependency:tree, which runs no phases at all. In CI I usually run mvn verify rather than clean install, because the workspace is fresh and installing to the local repository is not needed. And -DskipTests still compiles tests, while maven.test.skip skips compiling them too.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Maven guides
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "In Maven, what is the difference between a lifecycle, a phase and a goal? What actually...": https://scrivio-interview-library.netlify.app/java/maven/e680bea454/