Maven 简明教程

Maven - Build Life Cycle

What is Build Lifecycle?

构建生命周期是一个定义明确的阶段序列,该序列定义了执行目标的顺序。在此,阶段表示生命周期中的一个阶段。例如,一个典型的 Maven Build Lifecycle 包含以下阶段序列。

A Build Lifecycle is a well-defined sequence of phases, which define the order in which the goals are to be executed. Here phase represents a stage in life cycle. As an example, a typical Maven Build Lifecycle consists of the following sequence of phases.

Phase

Handles

Description

prepare-resources

resource copying

Resource copying can be customized in this phase.

validate

Validating the information

Validates if the project is correct and if all necessary information is available.

compile

compilation

Source code compilation is done in this phase.

Test

Testing

Tests the compiled source code suitable for testing framework.

package

packaging

This phase creates the JAR/WAR package as mentioned in the packaging in POM.xml.

install

installation

This phase installs the package in local/remote maven repository.

Deploy

Deploying

Copies the final package to the remote repository.

始终都有 prepost 阶段来注册 goals ,这些阶段必须在特定阶段之前或之后运行。

There are always pre and post phases to register goals, which must run prior to, or after a particular phase.

当 Maven 开始构建项目时,它会逐步执行一系列已定义的阶段并执行目标,这些目标已注册到各个阶段。

When Maven starts building a project, it steps through a defined sequence of phases and executes goals, which are registered with each phase.

Maven 具有以下三个标准生命周期−

Maven has the following three standard lifecycles −

  1. clean

  2. default(or build)

  3. site

goal 表示一项有助于构建和管理项目,并可能绑定到一个或多个构建阶段的特定任务。未绑定到任何构建阶段的目标可以通过直接调用在构建生命周期之外执行。

A goal represents a specific task which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.

执行顺序取决于目标和构建阶段的调用顺序。例如,考虑以下命令。 cleanpackage 参数是构建阶段,而 dependency:copy-dependencies 是目标。

The order of execution depends on the order in which the goal(s) and the build phase(s) are invoked. For example, consider the command below. The clean and package arguments are build phases while the dependency:copy-dependencies is a goal.

mvn clean dependency:copy-dependencies package

此处将首先执行 clean 阶段,然后执行 dependency:copy-dependencies goal ,最后执行 package 阶段。

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

Clean Lifecycle

当我们执行 mvn post-clean 命令时,Maven 将调用包含以下阶段的 clean 生命周期。

When we execute mvn post-clean command, Maven invokes the clean lifecycle consisting of the following phases.

  1. pre-clean

  2. clean

  3. post-clean

Maven clean 目标 (clean:clean) 绑定到 clean 生命周期中的 clean 阶段。它的 clean:cleangoal 通过删除构建目录来删除构建结果。因此,当执行 mvn clean 命令时,Maven 将删除构建目录。

Maven clean goal (clean:clean) is bound to the clean phase in the clean lifecycle. Its clean:cleangoal deletes the output of a build by deleting the build directory. Thus, when mvn clean command executes, Maven deletes the build directory.

我们可以通过在 clean 生命周期中的任何上述阶段中指定目标来自定义此行为。

We can customize this behavior by mentioning goals in any of the above phases of clean life cycle.

在以下示例中,我们将 maven-antrun-plugin:run 目标附加到 pre-clean、clean 和 post-clean 阶段。这将允许我们回显显示 clean 生命周期各个阶段的文本消息。

In the following example, We’ll attach maven-antrun-plugin:run goal to the pre-clean, clean, and post-clean phases. This will allow us to echo text messages displaying the phases of the clean lifecycle.

我们在 C:\MVN\project 文件夹中创建了一个 pom.xml。

We’ve created a pom.xml in C:\MVN\project folder.

<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>

现在,打开命令控制台,转到包含 pom.xml 的文件夹,并执行以下 mvn 命令。

Now open command console, go to the folder containing pom.xml and execute the following mvn command.

C:\MVN\project>mvn post-clean

Maven 将开始处理并显示 clean 生命周期中的所有阶段。

Maven will start processing and displaying all the phases of clean life cycle.

C:\MVN>mvn post-clean
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ project ---
[INFO] Executing tasks
     [echo] pre-clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ project ---
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ project ---
[INFO] Executing tasks
     [echo] clean phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.post-clean) @ project ---
[INFO] Executing tasks
     [echo] post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.740 s
[INFO] Finished at: 2021-12-10T20:03:53+05:30
[INFO] ------------------------------------------------------------------------

C:\MVN>

您可以尝试调整 mvn clean 命令,该命令将显示 pre-clean 和 clean。没有 post-clean 阶段将被执行。

You can try tuning mvn clean command, which will display pre-clean and clean. Nothing will be executed for post-clean phase.

Default (or Build) Lifecycle

这是 Maven 的主要生命周期,用于构建应用程序。它具有以下 21 个阶段。

This is the primary life cycle of Maven and is used to build the application. It has the following 21 phases.

Sr.No.

Lifecycle Phase & Description

1

validate Validates whether project is correct and all necessary information is available to complete the build process.

2

initialize Initializes build state, for example set properties.

3

generate-sources Generate any source code to be included in compilation phase.

4

process-sources Process the source code, for example, filter any value.

5

generate-resources Generate resources to be included in the package.

6

process-resources Copy and process the resources into the destination directory, ready for packaging phase.

7

compile Compile the source code of the project.

8

process-classes Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes.

9

generate-test-sources Generate any test source code to be included in compilation phase.

10

process-test-sources Process the test source code, for example, filter any values.

11

test-compile Compile the test source code into the test destination directory.

12

process-test-classes Process the generated files from test code file compilation.

13

test Run tests using a suitable unit testing framework (Junit is one).

14

prepare-package Perform any operations necessary to prepare a package before the actual packaging.

15

package Take the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file.

16

pre-integration-test Perform actions required before integration tests are executed. For example, setting up the required environment.

17

integration-test Process and deploy the package if necessary into an environment where integration tests can be run.

18

post-integration-test Perform actions required after integration tests have been executed. For example, cleaning up the environment.

19

verify Run any check-ups to verify the package is valid and meets quality criteria.

20

install Install the package into the local repository, which can be used as a dependency in other projects locally.

21

deploy Copies the final package to the remote repository for sharing with other developers and projects.

以下是几个与 Maven 生命周期相关的概念,值得提及:

There are few important concepts related to Maven Lifecycles, which are worth to mention −

  1. When a phase is called via Maven command, for example mvn compile, only phases up to and including that phase will execute.

  2. Different maven goals will be bound to different phases of Maven lifecycle depending upon the type of packaging (JAR / WAR / EAR).

在以下示例中,我们将 maven-antrun-plugin:run 目标附加到构建生命周期的几个阶段。这将允许我们通过回显文本消息来显示生命周期的阶段。

In the following example, we will attach maven-antrun-plugin:run goal to few of the phases of Build lifecycle. This will allow us to echo text messages displaying the phases of the lifecycle.

我们在 C:\MVN\project 文件夹中更新了 pom.xml。

We’ve updated pom.xml in C:\MVN\project folder.

<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>

现在打开命令控制台,转到包含 pom.xml 的文件夹并执行以下 mvn 命令。

Now open command console, go the folder containing pom.xml and execute the following mvn command.

C:\MVN\project>mvn compile

Maven 将开始处理并显示构建生命周期中直至编译阶段的阶段。

Maven will start processing and display phases of build life cycle up to the compile phase.

C:\MVN>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.validate) @ project ---
[INFO] Executing tasks
     [echo] validate phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-antrun-plugin:1.1:run (id.compile) @ project ---
[INFO] Executing tasks
     [echo] compile phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.033 s
[INFO] Finished at: 2021-12-10T20:05:46+05:30
[INFO] ------------------------------------------------------------------------

C:\MVN>

Site Lifecycle

Maven Site 插件通常用于创建新文档来创建报告、部署站点等。它具有以下阶段 -

Maven Site plugin is generally used to create fresh documentation to create reports, deploy site, etc. It has the following phases −

  1. pre-site

  2. site

  3. post-site

  4. site-deploy

在以下示例中,我们将 maven-antrun-plugin:run 目标附加到 Site 生命周期所有阶段。这将允许我们通过回显文本消息来显示生命周期的阶段。

In the following example, we will attach maven-antrun-plugin:run goal to all the phases of Site lifecycle. This will allow us to echo text messages displaying the phases of the lifecycle.

我们在 C:\MVN\project 文件夹中更新了 pom.xml。

We’ve updated pom.xml in C:\MVN\project folder.

<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-site-plugin</artifactId>
            <version>3.7</version>
         </plugin>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-project-info-reports-plugin</artifactId>
            <version>2.9</version>
         </plugin>
         <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>

现在打开命令控制台,转到包含 pom.xml 的文件夹并执行以下 mvn 命令。

Now open the command console, go the folder containing pom.xml and execute the following mvn command.

C:\MVN\project>mvn site

Maven 将开始处理并显示站点生命周期中直至站点阶段的阶段。

Maven will start processing and displaying the phases of site life cycle up to site phase.

C:\MVN>mvn site
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.projectgroup:project >----------------
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:3.0.0:run (id.pre-site) @ project ---
[INFO] Executing tasks
[WARNING]      [echo] pre-site phase
[INFO] Executed tasks
[INFO]
[INFO] --- maven-site-plugin:3.7:site (default-site) @ project ---
[WARNING] Input file encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[WARNING] No project URL defined - decoration links will not be relativized!
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.2 skin.
[INFO]
[INFO] --- maven-antrun-plugin:3.0.0:run (id.site) @ project ---
[INFO] Executing tasks
[WARNING]      [echo] site phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.323 s
[INFO] Finished at: 2021-12-10T20:22:31+05:30
[INFO] ------------------------------------------------------------------------

C:\MVN>