乐闻世界logo
搜索文章和话题

What are the phases of Maven's build lifecycle? How to use Maven commands to execute builds?

2月18日 21:29

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 phase command, such as mvn clean install

Practical Applications:

  • Development phase: mvn compile compile code
  • Testing phase: mvn test run tests
  • Packaging phase: mvn package package application
  • Local installation: mvn install install to local repository
  • Complete build: mvn clean package clean and package
  • Deployment: mvn deploy deploy to remote repository

Understanding Maven lifecycle is crucial for automated builds, continuous integration, and project deployment.

标签:Maven