Updated April 12, 2023
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
- Default Lifecycle
- Clean Lifecycle
- 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:
- 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
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.
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-
- Pre Clean
- Clean
- Post clean
The sequence of execution in clean commands.
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.
- Pre-site
- Site
- Post-site
- Site-deploy
Graphical Representation of Site Lifecycle Execution
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
We hope that this EDUCBA information on “Maven Life Cycle” was beneficial to you. You can view EDUCBA’s recommended articles for more information.