Java ProgrammingLearn About The Maven Build Life Cycle in Detail

Learn About The Maven Build Life Cycle in Detail

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.

mvn clean dependency: copy-dependencies package

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

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Aparajita>cd C:\work\project\maven_demo

C:\work\project\maven_demo>mvn post-clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven_demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven_demo ---
[INFO] Deleting C:\work\project\maven_demo\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.805 s
[INFO] Finished at: 2017-06-24T20:03:06-04:00
[INFO] Final Memory: 6M/75M
[INFO] ------------------------------------------------------------------------

C:\work\project\maven_demo>

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.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.eduonix</groupId>
  <artifactId>maven_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>maven_demo</name>
  <url>http://maven.apache.org</url>
<build>
<plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
   <executions>
      <execution>
         <id>id.pre-clean</id>
         <phase>pre-clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>pre-clean phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.clean</id>
         <phase>clean</phase>
         <goals>
          <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>clean phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.post-clean</id>
         <phase>post-clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>post-clean phase</echo>
            </tasks>
         </configuration>
      </execution>
   </executions>
   </plugin>
</plugins>
</build>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

• 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.

C:\work\project\maven_demo>mvn post-clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven_demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ maven_demo ---
[INFO] Executing tasks
     [echo] pre-clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven_demo ---
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ maven_demo ---
[INFO] Executing tasks
     [echo] clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.post-clean) @ maven_demo ---
[INFO] Executing tasks
     [echo] post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.328 s
[INFO] Finished at: 2017-06-24T20:27:56-04:00
[INFO] Final Memory: 6M/111M
[INFO] ------------------------------------------------------------------------

C:\work\project\maven_demo>

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.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.eduonix</groupId>
  <artifactId>maven_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>maven_demo</name>
  <url>http://maven.apache.org</url>
<build>
<plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
 <executions>
   <execution>
      <id>id.test</id>
      <phase>test</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
         <tasks>
            <echo>test phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
         <id>id.package</id>
         <phase>package</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
         <tasks>
            <echo>package phase</echo>
         </tasks>
      </configuration>
   </execution>
   <execution>
      <id>id.deploy</id>
      <phase>deploy</phase>
      <goals>
         <goal>run</goal>
      </goals>
      <configuration>
      <tasks>
         <echo>deploy phase</echo>
      </tasks>
      </configuration>
   </execution>
</executions>
   </plugin>
</plugins>
</build>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

• 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.

C:\work\project\maven_demo>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven_demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven_demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\work\project\maven_demo\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven_demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.515 s
[INFO] Finished at: 2017-06-24T20:55:07-04:00
[INFO] Final Memory: 8M/108M
[INFO] ------------------------------------------------------------------------

C:\work\project\maven_demo>

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.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.eduonix</groupId>
  <artifactId>maven_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>maven_demo</name>
  <url>http://maven.apache.org</url>
<build>
<plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
   <executions>
        <execution>
         <id>id.pre-site</id>
         <phase>pre-site</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>pre-site phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.site</id>
         <phase>site</phase>
         <goals>
         <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>site phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.post-site</id>
         <phase>post-site</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>post-site phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.site-deploy</id>
         <phase>site-deploy</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>site-deploy phase</echo>
            </tasks>
         </configuration>
      </execution>
   </executions>
   </plugin>
</plugins>
</build>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

• 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.

C:\work\project\maven_demo>mvn site
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven_demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-site) @ maven_demo ---
[INFO] Executing tasks
     [echo] pre-site phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-site-plugin:3.3:site (default-site) @ maven_demo ---
[INFO] configuring report plugin org.apache.maven.plugins:maven-project-info-reports-plugin:2.9
[INFO] Relativizing decoration links with respect to project URL: http://maven.apache.org
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin.
[INFO] Generating "Dependencies" report    --- maven-project-info-reports-plugin:2.9
[INFO] Generating "Dependency Convergence" report    --- maven-project-info-reports-plugin:2.9
[INFO] Generating "Dependency Information" report    --- maven-project-info-reports-plugin:2.9
[INFO] Generating "About" report    --- maven-project-info-reports-plugin:2.9
[INFO] Generating "Plugin Management" report    --- maven-project-info-reports-plugin:2.9
[INFO] Generating "Plugins" report    --- maven-project-info-reports-plugin:2.9
[INFO] Generating "Summary" report    --- maven-project-info-reports-plugin:2.9
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.site) @ maven_demo ---
[INFO] Executing tasks
     [echo] site phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.299 s
[INFO] Finished at: 2017-06-24T21:04:56-04:00
[INFO] Final Memory: 20M/216M
[INFO] ------------------------------------------------------------------------

C:\work\project\maven_demo>

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

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -