Table of contents
1.
Introduction
2.
Maven Lifecycle
3.
Maven Phases
3.1.
default 
3.2.
clean
3.3.
site 
4.
Maven Goals and Plugins
5.
Frequently Asked Questions
5.1.
What are the POM files?
5.2.
What is the role of POM in the Build Lifecycle?
5.3.
What is the relation between plugins and goals?
5.4.
Why should someone use Maven?
6.
Conclusion
Last Updated: Mar 27, 2024

Introduction to Maven Phases

Author Vanshika
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Maven is the build management tool that can automatically add dependencies, builds, and requirements, manage the classpath to the project, and generate war and jar files. POM (Project Object Model) forms the basis of Maven. It is a powerful tool for building and management of any Java-based project and web application. This article will teach you about the Maven phases, lifecycle, and goals. 

Maven phases and lifecycles

Before understanding the Maven phases, you must know Maven.

Maven Lifecycle

The central basis of Maven is its build lifecycles. The lifecycle is a sequence of Maven phases that executes one after another sequentially. It provides three built-in lifecycles which serve some purposes, viz- 

  • default: handles project deployment.
  • clean: handles project cleaning.
  • site: handles the creation of the project’s website.


The life cycles of Maven must execute in successive order. These life cycles are independent of each other. Every life cycle consists of a series of Maven phases.

Just like the trunk of a tree has various branches followed by many leaves. Similarly, each of these build life cycles of Maven is defined by a list of multiple phases, with each build phase having a series of several goals, as shown - 

Relation of Maven lifecycles, phases and goals.

Maven Phases

As the term suggests, the stages through which the build life cycle goes refer to the Maven phases. These phases are responsible for performing specific tasks termed as goals. Every build life cycle has its own set of phases executed sequentially.  

default 

default build lifecycle has 23 Maven phases in total. Some of the most important among these phases are:

Phases of default build lifecycle
  • validate: Validates the availability of the information and the correctness of the project.
     
  • compile: Compiles source code of the project (evaluates all files with .java extension to files with .class extension, which JVM can execute) and moves this file to /target folder.
     
  • test-compile: It is the same as the compile phase. The difference is it compiles for the tests folder.
     
  • package: It packs compiled project files into executable jar/war/ear files.
     
  • install: It puts an executable file into the local repository, where local projects can import it.
     
  • deploy: It copies an executable file to the remote repository for sharing with other developers.


Other phases include ‘initialize’, ‘prepare-package’, ‘process-resources’, ‘integration-test’, etc. 

All the Maven phases must execute sequentially. On running the command ‘mvn package’, all the previous phases in the sequence - ‘validate', ‘compile’, and ‘test-compile’ will also execute.

clean

‘clean’ build lifecycle has three Maven phases -  

clean build lifecycle

  • pre-clean: It executes those processes needed before the actual cleaning of the project.
     
  • clean: It removes all the files generated by the previous build.
     
  • post-clean: It executes the strategies required to finalize the project’s cleaning.

site 

‘site’ build lifecycle consists of four Maven phases - 

site build lifecycle

  • pre-site: It executes those processes needed before the actual site’s generation.
     
  • site: It generates the documentation of the project’s site.
     
  • post-site: It executes the functions required to finalize the site generation and prepare the site deployment.
     
  • site-deploy: It deploys the new site documentation generated to the given specific web server.
     

The phases in every built-in lifecycle execute in a specific order. It means that if we run a specific phase, say:

mvn <phase>


Here phase can be any of the Maven phases of the three life cycles, then it won’t just execute the phase mentioned in the command; instead, it will execute all the preceding phases as well.

For example, if we run the post-clean phase of the clean life cycle:

mvn post-clean


The pre-clean and clean phases will also execute along with the execution of the post-clean phase. Due to this reason, Maven phases are termed consecutive.

If we wish to skip the execution of any specific phase among the preceding phases, we need to define unique properties by using a particular command. To ignore the test phase, we need to add the following property to the command -

Dmaven.test.skip = true 


Ninjas, until now, we learned about Maven’s life cycles and phases. Since the beginning, you might be reading that these phases achieve some goals. So what are these goals? Now, as you know the phases of Maven, it’s the right time to dive into the goals they achieve.

Maven Goals and Plugins

A build phase consists of a sequence of goals. These goals represent a specific task leading to the project’s building and management. A plugin goal can be bound to either zero or more build phases. If a build phase has zero goals, then that build phase will not execute. But if there are one or many goals bound to the build phase, then it will run all those goals, and the order of execution will depend on the invoking order of goals and the build phases.

For example:

mvn post-clean dependency:copy-dependencies package


On executing this command, we will find that the post-clean phase will be executed at first, which means it will run all the phases, including itself and preceding the post-clean phase in the clean lifecycle. It follows the execution of the goal dependency:copy-dependencies before executing the final package phase (and all of its preceding phases of the default lifecycle).

Since there is a series of plugin goals for each phase, we won’t be able to remember. For ease, Maven provides us with the command - 

mvn help:describe -Dcmd=phasename


This helps to list all the goals bound to the specific phase. 

For example -

mvn help:describe -Dcmd=test


This command lists all the goals tied to the test phase.

Frequently Asked Questions

What are the POM files?

POM files stand for the Project Object Model files. These are the XML files that are read as pom.xml files and contain the information used by Maven for building the project, like dependencies, source directories, etc. In short, POM is the key to operating Maven.

What is the role of POM in the Build Lifecycle?

The configuration of pom.xml plays a vital role in setting up the project build lifecycle. It depends on the packaging value of pom.xml, which will be the set of goals that need to be executed by the Maven build.

What is the relation between plugins and goals?

The Maven plugin is the group of goals. Every goal is running with a specified plugin. Every plugin contains its own goals, which execute with only a specific plugin. For instance, the compiler plugin runs goals – compile and test compile.

Why should someone use Maven?

Maven handles all the dependencies, continuous builds, integrations, and testing. It provides an easy way of generating documentation from the source code and packaging compiled code into JAR or ZIP files. In the case of a dependency version update, one only has to update the version ID in the pom.xml file.

Conclusion

We learned that Maven is one of the essential tools for managing Java projects. It has a sequence of life cycles followed by a sequence of build phases, and further, it consists of a series of goals. It handles all the dependencies, continuous builds, integrations, and testing and provides an easy way of generating documentation from the source code. 

Recommended Readings - 

If you want to prepare for the interview, don’t worry; jump on the Maven interview questions.

For leading and upskilling yourself in the right direction, check out the guided paths by our one-stop platform Coding Ninjas Studio. Here you will learn Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more such courses. After availing of the benefits from our courses, you may check out your competency by referring to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning!

Live masterclass