Maven Life Cycle

Maven Life Cycle: When we hear the word Life Cycle, it’s nothing but the set of steps defined to execute the tasks. Similarly, the Maven build life cycle is nothing but executing the tasks and goals of the Maven project. In the life cycle process, after each step execution is successful, we get the desired output.

The Maven 2.0 version is focused on the Maven build lifecycle and states that Maven must follow different build life cycles to achieve the desired output after a successful Maven build lifecycle.

Post On:Maven Life Cycle
Post Type:Maven Tutorial
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

Maven Lifecycle Phases

Maven comes with three built-in maven phases:

Clean Life Cycle
Default Life Cycle
Site Life Cycle

You can execute more than one lifecycle at a time with Maven, and each lifecycle is independent of the others. Life cycles must be executed in sequential order, as shown below. The complete maven lifecycle is defined in the ‘component.xml’ file in the core module.

Each life cycle phase has a unique goal to be completed during that phase. Lifecycle methods such as ‘build’ and ‘test’ execute specific tasks to achieve the goal of that particular phase.

During the Maven Build Lifecycle phases, the following tasks/goals are performed:

  • Resource Preparation- On this task, maven copies of resources like configuration files to the build folder.
  • Compilation- Compiling source codes.
  • Packaging- Copy dependency JAR files to build the folder.
  • Runs unit tests.
Maven Life Cycle 1

Now, let’s try to understand the different phases in the maven life cycle in detail.

Clean Life Cycle: Maven Life Cycle

This Clean Life Cycle is used to clean up or delete the project directory name target and its contents and prepare it for fresh compiling and deployment. The command for this operation is mvn clean.

The mvn clean command runs a series of tasks that delete temporary files created during the build process. These commands, like mvn pre-clean and mvn post-clean commands, run internally when executing the mvn clean command.

  1. mvn pre-clean
  2. mvn clean
  3. mvn post-clean

When we are executing the mvn command, then maven will execute the current clean command and also execute its previous clean process.

Maven Life Cycle 2
  • When you execute that time, maven will execute the mvn pre-clean and mvn clean commands.
  • mvn pre-clean: It will execute the pre-clean only.
  • mvn clean: it will execute both commands’ pre-clean and mvn clean.
  • mvn post-clean: This will execute pre-clean, clean, and post-clean altogether.

Default Maven Life Cycle In Selenium & Java

This is the most typical and primary lifecycle of a maven. It starts with the validate phase, which checks that the Project is correct and all necessary information has been provided and ends with the deploy phase.

PhaseDescription
validateIt validates whether the Project is correct or not, and all necessary information is available to complete a build
initializeIt initializes the build state, for example, set properties or creating directories
generate-sourcesIt generates any source code for inclusion in the compilation
process-sourcesIt processes the source code, for example, to filter any values
generate-resourcesIt generates the resources for inclusion in the package.
process-resourcesIt copies and processes the resources into the destination directory, ready for packaging.
compileIt compiles the source code of the Project.
process-classesFor example, it post-processes the generated files from compilation to do bytecode enhancement on Java classes.
generate-test-sourcesIt generates any test source code for inclusion in compilation.
process-test-sourcesIt processes the test source code, for example, to filter any values.
generate-test-resourcesIt creates the resources for testing.
process-test-resourcesIt copies and processes the resources into the test destination directory.
test-compileIt compiles the test source code into the test destination directory
process-test-classesFor example, it post-processes the generated files from test compilation to do bytecode enhancement on Java classes.
testIt runs tests using a suitable unit testing framework. These tests should not require the code to be packaged or deployed.
prepare-packageIt performs any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package.
packageIt installs the package into the local repository for use as a dependency in other local projects.
pre-integration-testIt performs actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-testIt runs checks to verify the package is valid and meets quality criteria.
post-integration-testIt performs actions required after integration tests have been executed. This may include cleaning up the environment.
verifyIt processes and deploys the package, if necessary, into an environment where integration tests can be run.
installIf necessary, it processes and deploys the package into an environment where integration tests can be run.
deployIt is done in an integration or release environment and copies the final package to the remote repository for sharing with other developers and projects.

Note: If you execute the “mvn install” command, it will run all phases up to the install phase.

Each phase can be linked with zero or more plugin goals, and executing a particular phase is the same as running that phase’s plugin goals. For example, executing the install phase is like running the “install: install” goal.

But if you run the “mvn install” and “mvn install: install” commands, they’re not equivalent because “mvn install” will also execute all of the phases up to the install phase. So maven would also execute every goal associated with each prior phase–whereas “mvn install: install” will only execute this goal.

Here is the list of some of the most commonly used phased and attached goals.

Phaseplugin: goal
process-resourcesresources: resources
compilecompiler: compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire: test
packageejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
installinstall: install
deploydeploy: deploy

Site Maven Lifecycle

We can clean and compile the source code with the maven build automation tool and build a deployable application format. Apart from this, maven has a Site phase that can do more than the Clean and Default phases.

That phase is called the site phase, which has one of the crucial features of a maven: generating detailed documentation of the Project and preparing reports about the Project.

For Project documentation and site generation, in Maven, we have a specific lifecycle dedicated to that, which has four phases and that are:

  • pre-site: Executes processes before the project site generation
  • site: Generates the Project’s site documentation
  • post-site: Executes processes to finalize the site generation
  • site-deploy: Deploys the generated site to the web server

There are also plugin goals attached to the site life cycle phase, which are:

  • site ( site: site)
  • site-deploy ( site: deploy )

This command “mvn site” will generate the Javadocs for the Project. When you call this command, it is called “Doxia” document generation and other reporting plugins.

Conclusion:

We hope that you like the article. If you have any doubts or concerns, please write us in the comments section.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment