Maven Phases and Goals

·

3 min read

In this article we will explore different Maven build life cycles and their phases. Additionally, we would see the core relation between Goals and Phases.

Background

Maven is a build automation tool primarily used for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala and other languages. Maven is a part of Apache Software Foundation Based on the concept of project object model (POM), Maven can manage a project`s build, reporting and documentation from a central piece of information

Maven Build Life Cycle

The Maven build follows a specific life cycle to deploy and distribute the code. There are three built-in life cycles that maven offers:

  • default: main life cycle responsible for project deployment
  • clean: to clean the project and remove artifacts generated by the previous build
  • site: to create project documentation

Each life cycle consists of sequence of phases. The default build life cycle consists of 23 phases as it`s main build life cycle. On the other hand, clean life cycle consists of 3 phases, while the site life cycle is made up of 4 phases.

Maven Phase

A Maven phase represents a stage in the Maven build lifecycle. Each phase is responsible for a specific task. Here are the some of the most important phases in the default build lifecycle

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable testing framework. These tests should not require the code to be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as JAR
  • verify - run any checks on result of integration tests to ensure quality criteria is met
  • install - install the package into local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

So, how maven cycle works is, if you run a command for any of the lifecycle phases, it executes each default life cycle in order, before executing the command itself

Order of execution: Phases are executed in a specific order which is as:

validate >> compile >> test >> package >> verify >> install >> deploy

So, when you run the command mvn package, it runs the commands for all lifecycle phase till package

validate >> compile >> test >> package

And as for mvn install, it runs the commands for all lifecycle phase till install, which includes packages as well

validate >> compile >> test >> package >> verify >> install

Point worth mentioning here is difference between package and install command which is a major query, a newbie has when they start working with maven. install command does everything that package command does and some more (install the package into the local repository, for use as dependency

Maven Goal

Each phase is a sequence of goals, and each goal is responsible for a specific task. When we run a phase - all goals bound to this phase are executed in order. Here are some of the phases and default goals bound to them:

  • compiler:compile - the compile goal from the compiler plugin is bound to the compile phase
  • compiler:testCompile - the compile is bound to the test-compile phase
  • surefire:test - this is bound to test phase
  • install:install - this is bound to install phase
  • jar:jar - this is bound to package phase
  • war:war - this is bound to package phase

We can list all goals bound to a specific phase and their plugins using the command

mvn help:describe -Dcmd=PHASENAME

For example, to list all goals bound to the compile phase, we can run:

mvn help:describe -Dcmd=compile