Lifecycle Phases
The mvn
command accepts, apart from the options, only two things - goal or phase.
This chapter covers Phases which are related to Lifecycles and the subsequent chapters explain Goals which are related to Plugins.
Maven Lifecycles and Phases
To understand the concepts of lifecycle and phases, let’s recall 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 build task consisted of series of steps which are run one after another in predefined order. Maven calls the tasks as lifecycles and steps as phases.
By default, Maven defines three Lifecycles - default, clean and site and each Lifecycle consists of predefined Phases. Let’s go through them to understand the concept.
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 a phase in lifecycle again named clean.
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, clean and post-clean one after another in that order.
We can invoke any of these three phases and all preceding phases up to and including the invoked phase are executed sequentially.
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, the phases are just predefined steps which Maven invokes sequentially one after another and actual works is delegated to a plugin.
Default Lifecycle
The most used 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:
A project may contains resources such as properties, XML configuration files etc., The 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. The package phase creates JAR file of classes and resource files 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 the 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
- runs phases process-resources and then compile.mvn test
- runs phases process-resources, compile, process-test-resources, test-compile and finally test.mvn install
- runs phases process-resources, compile, process-test-resources, test-compile, test and finally install.
In the clean lifecycle we explained that lifecycle phases by themselves don’t have capabilities to accomplish any work. Here too same is true. 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 from your project folder. It triggers build failure and Maven shows the complete list of lifecycle phases.
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 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 anything and they rely on plugins to do any work.
In the next chapter, we look at Maven Plugins and how they bind with the Lifecycle Phases.