EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Maven Tutorial Maven Life Cycle
Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML

Maven Life Cycle

By Priya PedamkarPriya Pedamkar

Maven Life Cycle

Introduction to Maven Life Cycle

Maven is completely based on the central concept of its build Life cycles. These build life cycles are the processes for building and distributing a particular project is clearly defined in each phase.

Maven Life Cycle

Maven defines 3 build lifecycles

  1. Default Lifecycle
  2. Clean Lifecycle
  3. Site Lifecycle

Maven allows executing more than one lifecycle phase at a time. Each lifecycle is independent of each other. Life cycles have to be executed in sequential order as shown above. The complete maven lifecycle is defined in the ‘component.xml’ file in the core module. Each and every phase in lifecycle has a unique goal,

followings are the goals/tasks performed by lifecycle phases:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,560 ratings)
  • Resource Preparation- copies resources like configuration files to build folder.
  • Compilation- Compiling source codes.
  • Packaging- Copying dependency JAR files to build folder.
  • Runs unit tests.

Graphical Representation of Maven Lifecycle

Maven Life Cycle 1

1. Default Lifecycle

It is one of the most important Lifecycles of maven. In this lifecycle, Maven builds, tests and distributes. There are 22 phases/tasks performed in this lifecycle phase. Which are given in the following table?

S.No. Task Description
1 Validate This phase validates the project and gathers all project related information.
2 Initialize This phase initializes or sets properties for the build process.
3 Generate sources Generates required source codes for compiling the project.
4 Process Sources This phase copies and processes the sources into the destination directory and filters.
5 Generate resources Generate resources included in the packages.
6 Process resources Copies and processes the resources into a destination directory which are necessary for the packaging process.
7 Compile Compiles the source code.
8 Process classes This phase processes compiled source code or classes for optimization and byte code enhancement.
9 Generate-test-sources Generates test source code which included in the compilation phase.
10 Process-test-source Test the processes of the test source code and filter values.
11 Test-compile Tests the compiled source code and copies into the destination directory.
12 Process-test-classes Processes the generated files from the test compile phase.
13 Test This phase tests code using a suitable unit testing method.
14 Prepare-package This is the pre-packaging process. Performs necessary operation before the actual packaging process.
15 Package Gathers all compiled files and packages in a specified format.
16 Pre-integration-test This phase performs processes required before actual integration i.e. environment setup.
17 Integration-test Actual integration testing phase. Deploys the necessary packages for integration testing.
18 Post-integration-test In this phase, post-integration test processes are executed like clean up processes.
19 Verify This phase verifies the packages for validity and quality criteria.
20 Install This phase installs the packages into the local repository.
21 Deploy This process deploys the final packages into a remote repository.
22 Validate Validates the project code and packages for completion of the build process.

The following figure shows the process flow in the default lifecycle.

Maven Life Cycle 2

Example Maven commands in Default Lifecycle-

  • >mvn compile
  • >mvn validate
  • >mvn clean compile
  • >mvn package

2. Clean Lifecycle

In the Clean lifecycle phase, it performs the cleaning operation in which it deletes the build directory name target and its contents. To perform this operation we use command   >mvn clean. Clean lifecycle performs 3 types of clean operations-

  1. Pre Clean
  2. Clean
  3. Post clean

The sequence of execution in clean commands.

Maven Life Cycle 3

When we invoke any clean command maven will execute a current clean command with its previous clean process.

Note:

  • If we invoke-command >mvn clean. Maven will execute a pre-clean and clean phase.
  • Following are clean command sequences-
  • >mvn pre-clean –it will execute pre-clean only.
  • >mvn clean – it will execute pre-clean and clean process both.
  • >mvn post-clean – it will execute pre-clean, clean and post-clean all together.

3. Site Lifecycle

Mainly the site lifecycle handles the creation of project site documentation.

  • Maven Site Lifecycle phases.
  1. Pre-site
  2. Site
  3. Post-site
  4. Site-deploy

Graphical Representation of Site Lifecycle Execution

Maven Life Cycle 4

Sr.No. Name Description
1 Pre-site This phase executes the processes before the actual site.
2 Site This phase executes the actual site documentation process.
3 Post-site This phase executes the processes required to finalize the site generation and preparation for site deployment.
4 Site-deploy Deploys specific site generated documentation into the webserver.

Note:

  • Executing one phase in the site lifecycle which results to execute all previous phases prior to that phase.
  • If we invoke site command >mvn site –it will execute pre-site and site phases.
  • Sequential execution of site commands.
  • >mvn pre-site – it will execute the pre-site command.
  • >mvn site –it will invoke pre-site and site both.
  • >mvn post-site –it will invoke pre-site, site and post-site all together.
  • >mvn site-deploy –this command executes pre-site, site, post-site and deploy-site.
  • Maven site: site goal is usually referred to as the ‘site’ phase.
  • Mavens site: deploy goal is referred to as ‘site: deploy’ goal.
  • Typically >mvn clean site – is used to clean and generate new documents.
  • >mvn clean site-deploy –is usually used to clean the project site and deploy it.

Components.xml file

<Component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.binding.LegacyLifecycleParsingTestComponent</role>
<implementation>org.apache.maven.lifecycle.binding.LegacyLifecycleParsingTestComponent</implementation>
<configuration>
<lifecycles>
<lifecycle>
<id>default</id>
<phases>
<phase>validate</phase>
<phase>initialize</phase>
<phase>generate-sources</phase>
<phase>process-sources</phase>
<phase>generate-resources</phase>
<phase>process-resources</phase>
<phase>compile</phase>
<phase>process-classes</phase>
<phase>generate-test-sources</phase>
<phase>process-test-sources</phase>
<phase>generate-test-resources</phase>
<phase>process-test-resources</phase>
<phase>test-compile</phase>
<phase>process-test-classes</phase>
<phase>test</phase>
<phase>prepare-package</phase>
<phase>package</phase>
<phase>pre-integration-test</phase>
<phase>integration-test</phase>
<phase>post-integration-test</phase>
<phase>verify</phase>
<phase>install</phase>
<phase>deploy</phase>
</phases>
</lifecycle>
<lifecycle>
<id>clean</id>
<phases>
<phase>pre-clean</phase>
<phase>clean</phase>
<phase>post-clean</phase>
</phases>
<default-phases>
<clean>org.apache.maven.plugins:maven-clean-plugin:clean</clean>
</default-phases>
</lifecycle>
<lifecycle>
<id>site</id>
<phases>
<phase>pre-site</phase>
<phase>site</phase>
<phase>post-site</phase>
<phase>site-deploy</phase>
</phases>
<default-phases>
<site>org.apache.maven.plugins:maven-site-plugin:site</site>
<site-deploy>org.apache.maven.plugins:maven-site-plugin:deploy</site-deploy>
</default-phases>
</lifecycle>
</lifecycles>
</configuration>
</component>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>test-mapping</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<lifecycles>
<lifecycle>
<id>default</id>
<phases>
<package>org.apache.maven.plugins………</package>
<install> org.apache.maven.plugins………</install>
<deploy> org.apache.maven.plugins………</deploy>
</phases>
</lifecycle>
</lifecycles>
</configuration>
</component>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<implementation>org.apache.maven…….. </implementation>
<configuration>
<lifecycles>
<lifecycle>
<id>default</id>
<phases>
<package>
org.apache.maven.plugins………..
</package>
<install>org.apache.maven.plugins:maven-install-plugin:install</install>
<deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>
</lifecycle>
</lifecycles>
</configuration>
</component>
</components>
</component-set>

Conclusion

Mavens default plugin and lifecycle allows the user to perform build actions very easily. Since all lifecycles configuration is maintained in components.xml it gives more consistency and structured project implementation. Using configuration file user can set the configuration /goals at the initial stage only more modification is not required in the pom.xml file. Considering all three lifecycles in configuration file individual lifecycle phases can be understood and differentiated very easily without much complexity.

Recommended Articles

This is a guide to Maven Life Cycle. Here we discuss an introduction, Maven defines 3 build lifecycles default, clean and site lifecycle in detail. You can also go through our other related articles to learn more –

  1. Maven Plugins
  2. Maven Interview Questions
  3. Maven Commands
  4. How to Install Maven
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more