Maven Lifecycle

 

The Maven Build Lifecycle is a sequence of phases that define the order in which the goals (tasks) are executed to build and manage a project. Maven follows a well-defined standard lifecycle, which simplifies the build process and provides consistency across different projects.


What is a Build Lifecycle?

build lifecycle is essentially a well-defined sequence of phases that need to be executed to build a project. Each phase represents a stage in the lifecycle.

Maven has three built-in lifecycles:

  1. default – handles your project deployment (most commonly used).

  2. clean – handles project cleaning (deletes old builds).

  3. site – handles the creation of your project’s documentation.



1. Default Lifecycle (Core Build Lifecycle)

This is the main lifecycle and consists of 23 phases, out of which the most commonly used ones are:


PhaseDescription
validateValidates the project is correct and all necessary information is available.
initializeInitializes build state, sets properties.
generate-sourcesGenerates any source code needed before compilation.
process-sourcesProcesses the source code, like filtering.
generate-resourcesGenerates resources for inclusion in the package.
process-resourcesCopies and processes resources (like properties files) to the output directory.
compileCompiles the source code.
process-classesPost-processes the compiled classes (e.g., bytecode enhancement).
generate-test-sourcesGenerates test source code.
process-test-sourcesProcesses the test source code.
test-compileCompiles test source code.
process-test-classesPost-processes compiled test classes.
testRuns tests using a suitable unit testing framework.
prepare-packagePerforms operations before packaging like war/jar customization.
packagePackages the compiled code into a JAR/WAR.
pre-integration-testSetup steps before running integration tests.
integration-testRuns integration tests.
post-integration-testCleanup after integration tests.
verifyRuns checks to ensure the quality and correctness of the package.
installInstalls the package into the local Maven repository.
deployCopies the final package to the remote repository.



2. Clean Lifecycle

Used to clean the project before building. It has 3 phases:


PhaseDescription
pre-cleanExecuted before the actual project cleaning.
cleanDeletes the target/ directory (build output).
post-cleanExecuted after the cleaning is complete.




3. Site Lifecycle

Used to generate documentation for the project. It has 4 phases:


PhaseDescription
pre-siteExecuted before generating the site.
siteGenerates the project documentation.
post-siteExecutes operations needed after the site is generated.
site-deployDeploys the site to a web server.


Example Command Execution

mvn clean install

Above command will:

  • clean: delete the old target/ directory.
  • install: run the full default lifecycle (validate → compile → test → package → install).

No comments:

Post a Comment