Learn About The Maven Build Life Cycle in Detail

0
1193
Maven Build Life Cycle

In this article, we are going to discuss about the Maven build life cycle in detail. A Maven build life cycle is a sequence of phases in which goals are executed in a well-defined order. A phase is nothing but the representation of a stage in the life cycle. Let’s understand the build life cycle with the help of the following example.

S No.

Phase

Handles

Description

1.

Resources preparation

Copying of the project resource

Using Maven, we can customize the copying of Resource in this phase very easily.

2.

compilation

Compilation of the source code

In this phase, the compilation of the Source code is carried out.

3.

packaging

Packaging of the components

In this phase, the creation of the JAR, WAR or EAR package takes place as per the configuration details mentioned for packaging in the POM.xml.

4.

installation

Installation of the packages making it ready for the deployment

In this phase, the installation of the package in the local or remote maven repository takes place.

The following are the three standard lifecycles in Maven.
• clean
• default (or build)
• site

Goal in Maven
The goal in the Maven build is nothing but a particular task which is executed. Such task contributes to the project building and management. A goal could be associated with none to multiple build phases. When a goal is not associated with any build phase then it is executed by the direct invocation method which is outside the build life cycle.

The order of execution is dependent on the sequence in which the goals and the build phases are invoked. In the following command, the build phases are the clean and the package arguments whereas the dependency: copy-dependencies is a goal. As per the sequence in the command, the clean phase will be executed first, followed by the execution of the dependency: copy-dependencies goal, and the package phase.

Clean Lifecycle of Maven Build
Open a command prompt and navigate to the project path (C:\work\project\maven_demo) which we had created in the last tutorial. Now execute the “mvn post-clean” command as shown below. This command will direct Maven to invoke the clean lifecycle which consists of the following phases.
• pre-clean
• clean
• post-clean

Here, the clean: clean is a goal which is associated with the clean phase of the clean lifecycle. The clean: clean goal performs the deletion of the target directory which is the actual build directory.
Example: –
• Let’s execute the following command ‘mvn post-clean‘, after adding ‘maven-antrun-plugin: run’ plugin in the pom.xml file (shown below) in order to display the text messages for the phases of the clean lifecycle.

• Next, open a command prompt and navigate to the project root directory (C:\work\project\maven_demo) where the pom.xml file exists and execute the command ‘mvn post-clean‘ as shown below. In the output, we can observe that the Maven has started scanning the projects and displayed all the phases of the clean life cycle.

Note: – The mvn clean command will display only the pre-clean and clean goals and nothing will be executed as a part of the post-clean phase.
Default (or Build) Lifecycle of Maven Build
It is the main life cycle of Maven build which is used to build the application. There are 23 phases in the default or build lifecycle as follows.

1.

validate

This phase validates the project and all other project related necessary information which are required to complete the build process.

2.

initialize

This phase initializes the build state, e.g. set properties

3.

generate-sources

This phase generates the source code which may be required to be included in the compilation phase.

4.

process-sources

This phase processes the source code, e.g., filter any value.

5.

generate-resources

This phase generates the resources to be included in the package.

6.

process-resources

This phase copies and processes the resources into the destination directory to make it ready for the packaging phase.

7.

compile

This phase compiles the source code of the project.

8.

process-classes

This phase Post-processes the generated files from compilation, e.g. bytecode enhancement or optimization on Java classes.

9.

generate-test-sources

This phase generates the test source code which should be included in compilation phase.

10.

process-test-sources

This phase processes the test source code, e.g., filter any values.

11.

test-compile

This phase compiles the test source code into the test destination directory.

12.

process-test-classes

This phase processes the generated files from test code file compilation.

13.

test

This phase runs the tests using a suitable unit testing framework ( e.g., Junit, TestNG).

14.

prepare-package

This phase performs any operations necessary to prepare a package before the actual packaging process.

15.

package

This phase takes the compiled code and package it in its distributable format (i.e. JAR, WAR, or EAR file).

16.

pre-integration-test

This phase performs the actions required before integration tests are executed. E.g., the required environment set up.

17.

integration-test

This phase processes and deploys the package if necessary into an environment where integration tests can be executed.

18.

post-integration-test

This phase performs actions required after integration tests have been executed. E.g., the environment clean up.

19.

verify

This phase runs any check-ups to verify the package for its validity and quality criteria.

20.

install

This phase Installs the package into the local repository.

21.

deploy

This phase copies the final package to the remote repository for sharing with developers and projects.

22.

validate

This phase validates the project and other necessary information in order to complete the build process.

23.

initialize

This phase initializes the build state, e.g. set properties

There are few Maven concepts to keep in mind as follows.
• When we call a Maven phase through a maven command (say mvn compile) then the phases up to this phase (i.e. compile phase) will be executed.
• Depending on the packaging type (i.e. EAR, JAR, WAR, etc.), different Maven goals will bound to different Maven phases.
Example: –
• Let’s execute the following command ‘mvn compile‘, after adding ‘maven-antrun-plugin: run’ plugin in the pom.xml file (shown below) in order to display the text messages for the phases of the compile lifecycle.

• Next, open a command prompt and navigate to the project root directory (C:\work\project\maven_demo) where the pom.xml file exists and execute the command ‘mvn compile‘ as shown below. In the output, we can observe that the Maven has started scanning the projects and displayed all the phases up to the default or build life cycle.

Site Lifecycle of Maven Build
The Maven Site plugin is mostly used for the creation of reports, deploy site, etc. It has the following phases.
• pre-site
• site
• post-site
• site-deploy
Example: –
• Let’s execute the following command ‘mvn site‘, after adding ‘maven-antrun-plugin: run’ plugin in the pom.xml file (shown below) in order to display the text messages for the phases of the site lifecycle.

• Open a command prompt and navigate to the project root directory (C:\work\project\maven_demo) where the pom.xml file exists and execute the command ‘mvn site‘ as shown below. In the output, we can observe that the Maven has started scanning the projects and displayed all the phases up to the site life cycle.

Conclusion: –
In this article, we discussed about the Maven build life cycle in detail along with a suitable example of each of the phase.

LEAVE A REPLY

Please enter your comment!
Please enter your name here