Lifecycle Phases

Apart from the options, the mvn command accepts only two things - goal or phase.

Maven Lifecycle Phases - mvn usage description

This chapter covers Phases which are related to Lifecycles and in the next chapter explains Goals which are related to Plugins.

Maven Lifecycles and Phases

To understand the concepts of lifecycle and phase, let’s recap how we used to build Java projects by hand.

  • copy resources such as conf files to some build folder
  • compile the source files
  • copy dependency jars to build folder
  • run unit tests
  • package the build folder as jar

The job consisted of series of tasks or steps which we used to run one after another in some predefined order. Lifecycle and phases are just formal names for jobs and steps. Maven calls the jobs as lifecycles and tasks (or steps) as phases.

Maven defines three Lifecycles - default, clean and site and each Lifecycle consists of predefined Phases. Let’s go through them, one by one, to understand them better.

Clean Lifecycle

To clean the project, we use command mvn clean which deletes the build directory named target and its contents. The clean argument which we pass to mvn command is phase which is in lifecycle named clean.

Maven Lifecycle Phases clean lifecycle

The clean lifecycle contains three phases pre-clean, clean and post-clean .

When we invoke command mvn clean, Maven loads Clean Lifecycle and executes the phases pre-clean and clean.

We can invoke any of these three phases and all preceding phases up to and including the invoked phase are executed sequentially.

Maven Lifecycle Phases - lifecycle sequence

Lifecycle phases can’t do anything by themselves. For example, phase clean by itself doesn’t have ability or functionality to delete the build directory. It delegate the task to a plugin named maven-clean-plugin. So, lifecycles phases are just some predefined steps which Maven invokes sequentially one after another. As we can see, phases are similar to the steps in a job which are executed one after another.

 
 

Default Lifecycle

The most important of the three lifecycles is the Default Lifecycle . Maven uses default lifecycle to build, test and distribute the project. Default lifecycle contains 21 phases. Out of this big list, some of the frequently used ones are:

Maven Lifecycle Phases default lifecycle

Project may contain resources such as properties, XML configuration files etc., and phases process-resources and process-test-resources copy and process such resources files. and in a later chapter, we explain the resource management in detail.

The phases compile and test-compile complies the source Java files and test files respectively.

The phases package, install, and deploy are used to distribute the project. As we have already seen, the package phase creates JAR file of resources and compiled classes for distribution. The phase install, installs the artifacts of the project i.e jar and pom.xml to the local repository at $HOME/.m2 so that other projects can use them as dependencies. The phase deploy installs the artifacts of the project to a remote repository (probably on Internet) so that a wider group of projects can use it as dependency. We will cover these phases in a later chapter.

Since focus of this chapter is lifecycle phases, let’s get back to the default lifecycle. As we have seen in clean lifecycle, when a phase is invoked using mvn command, all preceding phases up to and including the invoked phase are executed sequentially. For example,

  • mvn compile - will run phases process-resources and then compile.

  • mvn test - will run phases process-resources, compile, process-test-resources, test-compile and finally test.

  • mvn install - will run phases process-resources, compile, process-test-resources, test-compile, test and finally install.

Like in clean lifecycle, in default lifecycle too, lifecycle phases by themselves don’t have capabilities to accomplish some task. For example, compile phase by itself can’t do anything but, it delegates compilation job to a plugin named maven-compiler-plugin.

In between the important phases shown above, there are many other minor phases such as validation, pre- and post- phases etc., For the complete list of phases in default life cycle, refer Maven - The Complete Reference - Default Lifecyle.

List of Phases

It is sufficient to remember the frequently used phases - clean, compile, test, package, install and deploy. We can always obtain the complete list by running mvn command without any argument. It triggers build failure and Maven shows the complete list of lifecycle phases.

Maven Lifecycle Phases default lifecycle

For clarity, we have formatted the output and highlighted the three lifecycle. Default lifecycle phases are marked in yellow, clean in red and site in blue.

Site Lifecycle

Site lifecycle generates project documentation and reports about the project. It contains four phases - pre-site, site, post-site, site-deploy. We can generate a site from a Maven project by running command - mvn site.

 
 

Summary

To summarize,

  • The Maven command mvn can accept only Lifecycle Phase or Plugin Goal as argument.

  • Maven comes with three lifecycles - default, clean and site .

  • each lifecycle is made up of lifecycle phases and in all, there are 28 phases - default 21, clean 3 and site 4.

  • when a lifecycle phase is invoked with mvn, all preceding phases up to and including the invoked phase are executed sequentially one after another.

  • lifecycle phases by themselves don’t have any capabilities to accomplish some task and they rely on plugins to carryout the task.

In the next chapter, we look at Maven Plugins and how they bind with Maven Lifecycle Phases.