Maven Build Lifecycle, Phases, and Goals

Filed Under: Maven
Maven Build Lifecycle Phase Goal

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.

  1. default: handles project build and deployment
  2. clean: handles project cleaning
  3. 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.

  • validate
  • compile
  • test
  • package
  • verify
  • install
  • deploy

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.


mvn dependency:tree

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.

The 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.

Phase Goal
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

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.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages