Maven — Lifecycle, Phases, Plugins and Goals

Core concepts in the build tool

Yet Another Software Engineer
3 min readDec 31, 2019

What is Maven?

Maven is a build and dependency management tool for software projects.

It provides a DSL to define, build, package and publish the project artifacts in a standard and configurable manner.

To put it in a simple words, it’s a tool to define how a project is built and how it’s dependencies are managed in order to build the project artifacts i.e JAR, WAR or EAR of a Java project.

A person building a project needs to just configure the project and specify a set of commands to build any Maven project, and the maven will ensure they get the results they desired.

Though developed and most suited for Java based projects, maven can be used by projects developed in other languages like C#, Ruby, Scala, Kotlin, Groovy and even Python.

Core Concepts

Maven is built around the concept of build Lifecycles, Phases, Plugins and Goals.

Lifecycle is an abstract concept around the expected stages in a project’s development lifetime. It is a sequence of named stages i.e Phases.

Maven has three defined Lifecycles — default (build), clean and site.

There are total of 28 phases defined by maven. Default lifecycle has 21 phases, clean has 3 and site has 4 phases.

Maven Lifecycles and Phases (Dark ones are frequently used)

Phases are the steps in a lifecycle which gets executed sequentially. Executing a phase means executing all the previous phases. Each phases may or may not perform some operations.

Plugin is a collection of Goals, which are basically specific tasks contributing to building and managing project. One can think of Plugins as classes and Goals as methods.

How it works?

Build plugins, goals and dependencies for a project are specified through the maven DSL — pom.xml file.

One can invoke either a maven phase or a goal while building a project.

To invoke a phase — mvn <phase-name>

mvn clean

To invoke a goal — mvn <plugin-name>:<goal-name>

mvn eclipse:eclipse

One can invoke multiple phases and goals in a single command.

mvn clean install

When a phase is invoked, all the preceding phases in the lifecycle gets executed sequentially.

A phase in itself doesn’t have any capabilities but they rely on the plugin and goals to accomplish the task at hand.

Maven has a plugin-based architecture.

Maven binds the lifecycle phases to plugin goals either through the default plugin goals or through the user defined plugin and goals declared in the POM file.

Maven binds some of the phases to default goals, which is what gets used when user doesn’t define anything for the project. Details of these binds can be found here.

Let’s look at what happens when “mvn clean compile” is run.

  • Maven figures out there are two lifecycles involved here i.e clean and default.
  • Next it checks for all the phases before clean and compile in the corresponding lifecycles i.e pre-clean phase for clean and validate, initialize, generate-sources, process-sources, generate-resources, process-resources phase for compile.
  • Once the phases to run are deduced, maven looks for all the plugin and goals associated with these phases eg. clean is associated with clean:clean, while compile is associated with compiler:compile.
  • Once all goals are deduced, maven runs them in the sequential manner. In a project with only default plugins, below would be the mvn command actually executed.
mvn clean:clean resources:resources compiler:compile

Summary

This article tries to demystify some of the core terms around which maven is built i.e Lifecycles, phases, plugins and goals. This one just scratches the maven’s surface but should be a good starting point for anyone starting with maven.

TL;DR:

Lifecycle is a collection of sequenced and named stages.

Phase is named stage in the build process which can execute zero or more actions.

Plugin is an artifact with one or more goals.

Goal is an action that can be executed.

--

--

Yet Another Software Engineer

I do Python, I do Java. I do monoliths, I do microservices. I do AWS, I do GCP. In other others, I’m Yet Another Software Engineer.