Maven's Build Lifecycle is a series of ordered phases, each representing a step in the build process. Maven defines three independent lifecycles: clean, default, and site.
1. Clean Lifecycle: Used for cleaning the project, includes three phases:
- pre-clean: perform work before cleaning
- clean: delete files generated by the previous build (target directory)
- post-clean: perform work after cleaning
2. Default Lifecycle: This is the most commonly used lifecycle, includes about 20 phases, main phases include:
- validate: validate that the project is correct and all necessary information is available
- compile: compile the project's source code
- test: test the compiled source code using a suitable unit testing framework
- package: package the compiled code into a distributable format (JAR, WAR, etc.)
- integration-test: process and deploy the package in an integration environment
- verify: run checks to verify the package is valid and meets quality standards
- install: install the package into the local repository for use by other local projects
- deploy: copy the final package to a remote repository for sharing with other developers and projects
3. Site Lifecycle: Used for generating project site documentation, includes:
- pre-site: perform work before generating the site
- site: generate project site documentation
- post-site: perform work after generating the site
- site-deploy: deploy the generated site to a server
Lifecycle Execution Rules:
- When executing a phase, all phases before it are automatically executed
- Different lifecycles are independent of each other, executing one lifecycle does not affect other lifecycles
- Can execute specific phases through
mvn phasecommand, such asmvn clean install
Practical Applications:
- Development phase:
mvn compilecompile code - Testing phase:
mvn testrun tests - Packaging phase:
mvn packagepackage application - Local installation:
mvn installinstall to local repository - Complete build:
mvn clean packageclean and package - Deployment:
mvn deploydeploy to remote repository
Understanding Maven lifecycle is crucial for automated builds, continuous integration, and project deployment.