Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Maven Build Lifecycle

Maven Build Lifecycle

The Maven build lifecycle defines the process of building and releasing a project, and contains three standard lifecycles:

Each lifecycle is made up of multiple phases, which run in order during execution.

1. The clean lifecycle

The clean lifecycle is responsible for cleaning the project's temporary files and directories (mainly target/) and contains the following main phases:

Stage Description Automatically bound plugin goals
pre-clean Preparation work before cleaning No default binding
clean Deletetarget/directory maven-clean-plugin:clean
post-clean Wrap-up work after cleaning No default binding

** Common commands:**



mvn clean      # 执行clean阶段(包含pre-clean和clean)

2. The default lifecycle

The default lifecycle is the most important lifecycle in Maven, containing all the core steps of project building and deployment.

A typical Maven build lifecycle is composed of a sequence of the following phases:

Stage Handling Description
Validate validate Validate the project Verify that the project is correct and that all necessary information is available
Compile compile Perform compilation Source code compilation is completed in this phase
Test Test Testing Run tests using an appropriate unit testing framework (e.g., JUnit).
Package package package Package the compiled code into a distributable format, such as JAR or WAR
Verify verify Checks Check the results of integration tests to ensure that quality standards are met
Install install Installation Install the packaged project into the local repository for use by other projects
Deploy deploy Deployment Copy the final project package to a remote repository, to share it with other developers and projects

To complete the default lifecycle, these phases (including other lifecycle phases not listed above) will be executed in order.

Phase dependency relationships:

Common commands:

mvn compile     # 编译主代码

mvn test        # 运行测试

mvn package     # 打包(生成target/*.jar)

mvn install     # 安装到本地仓库(~/.m2/repository)

mvn deploy      # 部署到远程仓库(需配置distributionManagement)

3. The site lifecycle

The site lifecycle is used to generate project site documentation:

Stage Description Automatically bound plugin goals
pre-site Preparation before site generation No default binding
site Generate project site documentation maven-site-plugin:site
post-site Wrap-up after site generation No default binding
site-deploy Deploy the site to a server maven-site-plugin:deploy

** Common commands:**



mvn site        # 生成站点(输出在target/site/)

mvn site-deploy # 部署站点(需配置site URL)

Lifecycle Summary

Maven has the following three standard lifecycles:

1. Clean lifecycle:

2. Default lifecycle (also called the Build lifecycle):

3. Site lifecycle:

Build Phases Are Composed of Plugin Goals

A plugin goal represents a specific task (more fine-grained than a build phase), which helps in project building and management. These goals may be bound to multiple phases or to none. Goals not bound to any build phase can be executed by direct invocation outside the build lifecycle. The execution order of these goals depends on the order in which the goals and build phases are invoked.

For example, consider the following command:

clean and pakage are build phases, and dependency:copy-dependencies is a goal



mvn clean dependency:copy-dependencies package

Here the clean phase will be executed first, then the dependency:copy-dependencies goal will be executed, and finally the package phase will be executed.


1. Clean lifecycle

When we execute the mvn post-clean command, Maven invokes the clean lifecycle, which contains the following phases:

The clean in mvn clean is the clean above. In a lifecycle, when a certain phase is run, all the phases before it are also run. That is to say, if mvn clean is executed, the following two lifecycle phases will run:

pre-clean, clean

If we run mvn post-clean, the following three lifecycle phases will run:

pre-clean, clean, post-clean

We can modify this part of the operation behavior by defining goals in any phase of the clean lifecycle above.

In the example below, we add the maven-antrun-plugin:run goal to the pre-clean, clean, and post-clean phases. This way we can display text messages in each phase of the clean lifecycle.

We have created a pom.xml file in the C:\MVN\project directory.

<

project

xmlns

=

"

http://maven.apache.org/POM/4.0.0

"

xmlns:xsi

=

"

http://www.w3.org/2001/XMLSchema-instance

"

xsi:schemaLocation

=

"

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

"

<

modelVersion

4.0.0

modelVersion

<

groupId

com.companyname.projectgroup

groupId

<

artifactId

project

artifactId

<

version

1.0

version

<

build

<

plugins

<

plugin

<

groupId

org.apache.maven.plugins

groupId

<

artifactId

maven-antrun-plugin

artifactId

<

version

1.1

version

<

executions

<

execution

<

id

id.pre-clean

id

<

phase

pre-clean

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

pre-clean phase

echo

tasks

configuration

execution

<

execution

<

id

id.clean

id

<

phase

clean

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

clean phase

echo

tasks

configuration

execution

<

execution

<

id

id.post-clean

id

<

phase

post-clean

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

post-clean phase

echo

tasks

configuration

execution

executions

plugin

plugins

build

project

Now open the command console, navigate to the directory where pom.xml is located, and execute the following mvn command.



C:\MVN\project>mvn post-clean

Maven will start processing and display all the phases of the clean lifecycle.



[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------

[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0

[INFO]    task-segment: [post-clean]

[INFO] ------------------------------------------------------------------

[INFO] [antrun:run {execution: id.pre-clean}]

[INFO] Executing tasks

     [echo] pre-clean phase

[INFO] Executed tasks

[INFO] [clean:clean {execution: default-clean}]

[INFO] [antrun:run {execution: id.clean}]

[INFO] Executing tasks

     [echo] clean phase

[INFO] Executed tasks

[INFO] [antrun:run {execution: id.post-clean}]

[INFO] Executing tasks

     [echo] post-clean phase

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------

[INFO] Total time: < 1 second

[INFO] Finished at: Sat Jul 07 13:38:59 IST 2012

[INFO] Final Memory: 4M/44M

[INFO] ------------------------------------------------------------------

You can try modifying the mvn clean command to display pre-clean and clean, while performing no action in the post-clean phase.


2. Default (Build) lifecycle

This is the main lifecycle of Maven, used to build applications, and includes the following 23 phases:

Lifecycle phase Description
validate (validation) Verify that the project is correct and that all necessary information is available to complete the project's build process.
initialize (initialization) Initialize the build state, such as setting property values.
generate-sources (generate source code) Generate any source code to be included in the compilation phase.
process-sources (Process Sources) Process sources, for example, filter any values.
generate-resources (Generate Resources) Generate resource files to be included in the project package.
process-resources (Process Resources) Copy and process resources to the target directory, preparing for the packaging phase.
compile (Compile) Compile the project's source code.
process-classes (Process Classes) Process files generated by compilation, for example, bytecode improvement and optimization of Java class files.
generate-test-sources (Generate Test Sources) Generate any test source code to be included in the compilation phase.
process-test-sources (Process Test Sources) Process test sources, for example, filter any values.
generate-test-resources (Generate Test Resources) Create resource files for testing.
process-test-resources (Process Test Resources) Copy and process test resources to the target directory.
test-compile (Compile Test Sources) Compile test source code to the test target directory.
process-test-classes (Process Test Classes) Process files generated by compiling test sources.
test (Test) Run tests using an appropriate unit testing framework (Juint is one of them).
prepare-package (Prepare Package) Before actual packaging, perform any necessary operations to prepare for packaging.
package (Package) Package the compiled code into distributable format files, such as JAR, WAR, or EAR files.
pre-integration-test (Pre-Integration Test) Perform necessary actions before executing integration tests. For example, set up the required environment.
integration-test (Integration Test) Process and deploy the project to an environment where integration tests can run.
post-integration-test (Post-Integration Test) Perform necessary actions after integration tests complete. For example, clean up the integration test environment.
verify (Verify) Run any checks to verify the project package is valid and meets quality standards.
install (Install) Install the project package into the local repository so it can be used as a dependency by other local projects.
deploy (Deploy) Copy the final project package to a remote repository to share with other developers and projects.

There are some important concepts related to the Maven lifecycle that need explanation:

When a phase is invoked via a Maven command, such as mvn compile, only that phase and all phases before it (including that phase) are executed.

Different maven goals are bound to different Maven lifecycle phases depending on the packaging type (JAR / WAR / EAR).

In the following example, we add the maven-antrun-plugin:run goal to some phases of the Build lifecycle. This lets us display text information about the lifecycle.

We have updated the pom.xml file in the C:\MVN\project directory.

<

project

xmlns

=

"

http://maven.apache.org/POM/4.0.0

"

xmlns:xsi

=

"

http://www.w3.org/2001/XMLSchema-instance

"

xsi:schemaLocation

=

"

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

"

<

modelVersion

4.0.0

modelVersion

<

groupId

com.companyname.projectgroup

groupId

<

artifactId

project

artifactId

<

version

1.0

version

<

build

<

plugins

<

plugin

<

groupId

org.apache.maven.plugins

groupId

<

artifactId

maven-antrun-plugin

artifactId

<

version

1.1

version

<

executions

<

execution

<

id

id.validate

id

<

phase

validate

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

validate phase

echo

tasks

configuration

execution

<

execution

<

id

id.compile

id

<

phase

compile

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

compile phase

echo

tasks

configuration

execution

<

execution

<

id

id.test

id

<

phase

test

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

test phase

echo

tasks

configuration

execution

<

execution

<

id

id.package

id

<

phase

package

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

package phase

echo

tasks

configuration

execution

<

execution

<

id

id.deploy

id

<

phase

deploy

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

deploy phase

echo

tasks

configuration

execution

executions

plugin

plugins

build

project

Now open the command console, navigate to the directory containing pom.xml, and execute the following mvn command.



C:\MVN\project>mvn compile

Maven will start processing and display each phase of the build lifecycle up to the compile phase.



[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------

[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0

[INFO]    task-segment: [compile]

[INFO] ------------------------------------------------------------------

[INFO] [antrun:run {execution: id.validate}]

[INFO] Executing tasks

     [echo] validate phase

[INFO] Executed tasks

[INFO] [resources:resources {execution: default-resources}]

[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,

i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory C:\MVN\project\src\main\resources

[INFO] [compiler:compile {execution: default-compile}]

[INFO] Nothing to compile - all classes are up to date

[INFO] [antrun:run {execution: id.compile}]

[INFO] Executing tasks

     [echo] compile phase

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------

[INFO] Total time: 2 seconds

[INFO] Finished at: Sat Jul 07 20:18:25 IST 2012

[INFO] Final Memory: 7M/64M

[INFO] ------------------------------------------------------------------

Command Line Invocation

In a development environment, use the following command to build and install the project to the local repository



mvn install

Before the install phase, this command executes the phases of the default lifecycle in order (validate, compile, package, etc.); we only need to invoke the last phase, here install.

In a build environment, use the following invocation to cleanly build and deploy the project to a shared repository



mvn clean deploy

This command can also be used in a multi-module case, i.e., a project containing multiple sub-projects; Maven executes the clean command on each sub-project, then executes the deploy command.


3. Site Lifecycle

The Maven Site plugin is generally used to create new report documents, deploy sites, etc.

The site phase and site-deploy phase are commonly used here to generate and publish Maven sites. This is a quite powerful feature of Maven; managers like it because documentation and statistics are generated automatically and look good. In the following example, we add the maven-antrun-plugin:run goal to all phases of the Site lifecycle. This lets us display all text information about the lifecycle.

We have updated the pom.xml file in the C:\MVN\project directory.

<

project

xmlns

=

"

http://maven.apache.org/POM/4.0.0

"

xmlns:xsi

=

"

http://www.w3.org/2001/XMLSchema-instance

"

xsi:schemaLocation

=

"

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd

"

<

modelVersion

4.0.0

modelVersion

<

groupId

com.companyname.projectgroup

groupId

<

artifactId

project

artifactId

<

version

1.0

version

<

build

<

plugins

<

plugin

<

groupId

org.apache.maven.plugins

groupId

<

artifactId

maven-antrun-plugin

artifactId

<

version

1.1

version

<

executions

<

execution

<

id

id.pre-site

id

<

phase

pre-site

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

pre-site phase

echo

tasks

configuration

execution

<

execution

<

id

id.site

id

<

phase

site

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

site phase

echo

tasks

configuration

execution

<

execution

<

id

id.post-site

id

<

phase

post-site

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

post-site phase

echo

tasks

configuration

execution

<

execution

<

id

id.site-deploy

id

<

phase

site-deploy

phase

<

goals

<

goal

run

goal

goals

<

configuration

<

tasks

<

echo

site-deploy phase

echo

tasks

configuration

execution

executions

plugin

plugins

build

project

Now open the command console, navigate to the directory containing pom.xml, and execute the following mvn command.



C:\MVN\project>mvn site

Maven will start processing and display each phase of the site lifecycle up to the site phase.



[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------

[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0

[INFO]    task-segment: [site]

[INFO] ------------------------------------------------------------------

[INFO] [antrun:run {execution: id.pre-site}]

[INFO] Executing tasks

     [echo] pre-site phase

[INFO] Executed tasks

[INFO] [site:site {execution: default-site}]

[INFO] Generating "About" report.

[INFO] Generating "Issue Tracking" report.

[INFO] Generating "Project Team" report.

[INFO] Generating "Dependencies" report.

[INFO] Generating "Project Plugins" report.

[INFO] Generating "Continuous Integration" report.

[INFO] Generating "Source Repository" report.

[INFO] Generating "Project License" report.

[INFO] Generating "Mailing Lists" report.

[INFO] Generating "Plugin Management" report.

[INFO] Generating "Project Summary" report.

[INFO] [antrun:run {execution: id.site}]

[INFO] Executing tasks

     [echo] site phase

[INFO] Executed tasks

[INFO] ------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------

[INFO] Total time: 3 seconds

[INFO] Finished at: Sat Jul 07 15:25:10 IST 2012

[INFO] Final Memory: 24M/149M

[INFO] ------------------------------------------------------------------```