Maven Build Lifecycle
Maven Build Lifecycle
In this post, you will learn the Maven Build Lifecycle. Various Maven build phases and settings files. Apache Maven is a tool for managing Java-based projects. A core part of Maven is its Build Lifecycle, which defines the process for building, testing, and deploying software.
A build lifecycle is a sequence of phases that automate tasks like compiling code, running tests, and packaging software. Maven organizes these tasks into logical groups, ensuring consistency and efficiency.
Maven Settings file
● Global settings -> Located in ${M2_HOME}/conf/settings.xml
● User’s settings -> Located in ${HOME}/.m2/settings.xml
● Contains configuration for local installation of Maven, such as repositories and profiles
Maven Archetypes
Maven archetypes allow us to create Maven projects on the fly.
● A rapid way to build sample projects
● Many archetypes available in remote repositories
● To use an archetype, run mvn archetype:generate
● Interactive, allowing us to choose an archetype and specify groupId, artifactId, and version.
● Generates a sample project directory, with some specifics that you provide.
● Some archetypes need properties supplied command-line as “-D<propName>=<propValue>”.
Maven Build Lifecycle
● A goal is a task performed by a plugin, such as compiler:compile and install:install-file
● The goals are bound to life cycle phases, in which they are executed.
● Phases and goals can both be executed directly from the command-line.
Maven has three built-in lifecycles:
- default: Handles project deployment (build, test, package).
- clean: Removes temporary files from previous builds.
- site: Generates documentation for the project.
Default Lifecycle Phases
The default lifecycle includes key phases executed in order:
- validate: Checks project setup.
- compile: Converts source code to bytecode.
- test: Runs unit tests.
- package: Bundles code into a JAR/WAR file.
- verify: Checks test results.
- install: Saves the package to your local repository.
- deploy: Shares the package to a remote repository.
Clean Lifecycle Phases
The clean lifecycle removes build artifacts:
- pre-clean: Prepares for cleanup.
- clean: Deletes the build directory.
- post-clean: Finalizes cleanup tasks.
Site Lifecycle Phases
The site lifecycle creates project documentation:
- pre-site: Prepares for documentation generation.
- site: Generates site reports (e.g., code coverage).
- post-site: Finalizes site files.
- site-deploy: Publishes documentation to a server.
POM.xml
Running a build phase
Running various build phases are either by running commands or from the IDE like Eclipse or IntelliJ.
validate – validate the project is correct and all necessary information of the project is available
compile – compile the source code files of the project
test – test the compiled source code using a suitable unit testing framework.
package – take the compiled code and package it in its distribution format, such as war, ear, jar.
integration-test – process and deploy the package if necessary into an environment where integration tests environment
verify – run any checks to verify the package is valid and meets quality
install – install the package into the local repository, for use as a dependency in other projects locally
deploy – done in an integration or release environment, copies the final package to the remote repository
clean life-cycle
clean– removes all files in previous runs.
Site
Site– generates all the project documentation
/> mvn install
We can use mvn install to package the project and deploy it to the local repository automatically.
When we run “install” phase, all above phases “validate“, “compile“, “test“, “package“, “integration-test“, “verify” phase, including the current “install” phase will be executed orderly.
We can run “clean” and “install” together so that the latest project to your local repository.
/> mvn clean install
Maven Tutorials
Maven Tutorials on this website can be found at: