Maven Life Cycle

Maven Life Cycle: When we talk about a Maven Life Cycle, we mean a set of steps to do tasks. The Maven build life cycle is a set of steps to complete the tasks and goals of a Maven project. After each step, if everything goes well, we get the expected result.

In Maven version 2.0, the focus is on the build lifecycle. It says that Maven needs to follow different build life cycles to get the right result after completing a build.

Post On:Maven Life Cycle
Post Type:Maven Tutorial
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience

Maven Lifecycle Phases

Maven comes with three built-in maven phases:

Clean Life Cycle
Default Life Cycle
Site Life Cycle

You can run more than one lifecycle at a time with Maven, and each one works separately from the others. However, they must be done in a specific order. The entire lifecycle is described in the “component.xml” file in Maven’s core module.

Each phase has a specific goal. For example, phases like ‘build’ and ‘test’ do certain tasks to reach the phase’s goal.

Tasks/Goals During Maven Build Lifecycle

Here are some tasks done during the Maven build lifecycle:

  • Resource Preparation: Copies of resources like configuration files are added to the build folder.
  • Compilation: Compiles the source code.
  • Packaging: Puts dependency JAR files into the build folder.
  • Running Unit Tests: Tests the code.
Maven Life Cycle 1

Let’s look at the different phases in more detail:

Clean Life Cycle

The Clean Life Cycle is used to delete or clean the project directory (usually called ‘target’) and its contents to get ready for new compiling and deployment. The command for this is “mvn clean“.

The mvn clean command does several tasks to delete temporary files made during the build. Commands like “mvn pre-clean” and “mvn post-clean” also run when you use mvn clean.

  • mvn pre-clean: Runs tasks before cleaning.
  • mvn clean: Cleans the build and also runs pre-clean tasks.
  • mvn post-clean: Runs tasks after cleaning.

When you run the mvn clean command, it does the pre-clean, clean, and post-clean steps in order.

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 pre-clean and mvn clean commands.
  • mvn post-clean: This will execute pre-clean, clean, and post-clean altogether.

Default Maven Life Cycle

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 compiles the project’s source code.
process-sourcesIt processes the source code, for example, to filter any values
generate-resourcesIt generates the resources for inclusion in the package.
process-resourcesIt initializes the build state, for example, by setting properties or creating directories.
compileIt is done in an integration or release environment, and the final package is copied to the remote repository for sharing with other developers and projects.
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 initializes the build state, for example, by setting properties or creating directories

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” would only execute this maven 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

With Maven, you can clean and compile the source code and create a deployable application. Besides that, Maven has a “Site” phase, which does more than the Clean and Default phases.

The Site phase is important because it generates detailed documentation of the project and prepares reports. Maven has a specific lifecycle for this, with four phases.

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

Understanding the Maven build lifecycle is important for managing and automating the build process in Java projects. Whether you are cleaning up your project, compiling code, or creating documentation, Maven’s structured approach ensures that each task is done correctly and in the right order.

By learning the Clean, Default, and Site phases, you can make your development process smoother and create better applications.

Share this knowledge with your friends and colleagues to help them improve their development skills and use Maven’s powerful tools. Please spread the word and help others discover the full potential of Maven in their projects!

FAQ On Interview

[sp_easyaccordion id=”67662″]

Avatar for Softwaretestingo Editorial Board

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