Table of Contents
What is Maven Build Lifecycle?
When we build a Maven project, it executes a set of clearly defined tasks based on the project pom.xml configuration and the command-line options. This standard set of tasks creates the maven build lifecycle.
The benefit of a clearly defined lifestyle is that we have to remember only a few sets of commands to compile, build, install, and deploy our projects.
Recommended Reading: 20+ Maven Commands and Options (Cheat Sheet)
Built-in Build Lifecycles
There are three built-in build lifecycles.
- default: handles project build and deployment
- clean: handles project cleaning
- site: handles the creation of project site documentation
Maven Build Phases
Maven build lifecycle goes through a set of stages, they are called build phases. For example, the default lifecycle is made up of the following phases.
The build phases are executed sequentially. When we run a maven build command, we specify the phase to be executed. Any maven build phases that come before the specified phase is also executed. For example, if we run
mvn package then it will execute validate, compile, test, and package phases of the project.
Maven Build Goals
A build phase is made up of a set of goals. Maven goals represent a specific task that contributes to the building and managing of a project.
Sometimes, a maven goal is not bound to a build phase. We can execute these goals through the command line.
The syntax to execute a goal is:
$ mvn plugin-prefix:goal $ mvn plugin-group-id:plugin-artifact-id[:plugin-version]:goal
Here is an example to execute the dependency tree goal from the command line. It’s not part of any build phases.
Role of POM in Build Lifecycle
The maven builds executed through the command line runs a set of phases and goals. The pom.xml configuration plays a major role in setting up the project build lifecycle.
packaging value of pom.xml file defines the set of goals to be executed by the maven build. For example, if it’s
jar then the following phases and goals will be executed.
We can also configure goals in the pom.xml file using the plugins element. This is mostly required when you have created a custom plugin and want to execute any specific goal for a build phase.