Maven Goals and Phases – Maven目标和阶段

最后修改: 2018年 9月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll explore different Maven build lifecycles and their phases.

在本教程中,我们将探讨不同的Maven构建生命周期及其阶段。

We’ll also discuss the core relation between Goals and Phases.

我们还将讨论目标和阶段之间的核心关系。

2. Maven Build Lifecycle 

2.Maven构建生命周期

The Maven build follows a specific lifecycle to deploy and distribute the target project.

Maven构建遵循特定的生命周期来部署和分发目标项目。

There are three built-in lifecycles:

有三个内置的生命周期。

  • default: the main lifecycle, as it’s responsible for project deployment
  • clean: to clean the project and remove all files generated by the previous build
  • site: to create the project’s site documentation

Each lifecycle consists of a sequence of phases. The default build lifecycle consists of 23 phases, as it’s the main build lifecycle.

每个生命周期由一系列阶段组成。默认构建生命周期包括23个阶段,因为它是主要的构建生命周期。

On the other hand, the clean life cycle consists of 3 phases, while the site lifecycle is made up of 4 phases.

另一方面,清洁生命周期由3个阶段组成,而网站生命周期则由4个阶段组成。

3. Maven Phase

3.Maven阶段

A Maven phase represents a stage in the Maven build lifecycle. Each phase is responsible for a specific task.

Maven阶段代表Maven构建周期的一个阶段。每个阶段都负责一项特定的任务。

Here are some of the most important phases in the default build lifecycle:

以下是默认构建生命周期中的一些最重要的阶段。

  • validate: check if all information necessary for the build is available
  • compile: compile the source code
  • test-compile: compile the test source code
  • test: run unit tests
  • package: package compiled source code into the distributable format (jar, war, …)
  • integration-test: process and deploy the package if needed to run integration tests
  • install: install the package to a local repository
  • deploy: copy the package to the remote repository

For the full list of each lifecycle’s phases, check out the Maven Reference.

关于每个生命周期阶段的完整列表,请查看Maven参考文献

Phases are executed in a specific order. This means that if we run a specific phase using the command:

阶段是按特定顺序执行的。这意味着,如果我们使用命令运行一个特定的阶段。

mvn <PHASE>

It won’t only execute the specified phase, but all the preceding phases as well.

它不仅会执行指定的阶段,也会执行所有前面的阶段。

For example, if we run the deploy phase, which is the last phase in the default build lifecycle, it’ll execute all the phases before the deploy phase as well, which is the entire default lifecycle:

例如,如果我们运行deploy阶段,这是default构建生命周期的最后一个阶段,它也会执行deploy阶段之前的所有阶段,也就是整个default生命周期。

mvn deploy

4. Maven Goal 

4.Maven目标

Each phase is a sequence of goals, and each goal is responsible for a specific task.

每个阶段都是一连串的目标,每个目标都负责一个具体的任务。

When we run a phase, all goals bound to this phase are executed in order.

当我们运行一个阶段时,所有与这个阶段绑定的目标都会按顺序执行。

Here are some of the phases and default goals bound to them:

以下是一些阶段和与之相联系的默认目标。

  • compiler:compile – the compile goal from the compiler plugin is bound to the compile phase
  • compiler:testCompile is bound to the test-compile phase
  • surefire:test is bound to the test phase
  • install:install is bound to the install phase
  • jar:jar and war:war is bound to the package phase

We can list all goals bound to a specific phase and their plugins using the command:

我们可以用命令列出所有绑定在特定阶段的目标和它们的插件。

mvn help:describe -Dcmd=PHASENAME

For example, to list all goals bound to the compile phase, we can run:

例如,要列出所有绑定在编译阶段的目标,我们可以运行。

mvn help:describe -Dcmd=compile

Then we’d get the sample output:

然后我们会得到样本输出。

compile' is a phase corresponding to this plugin:
org.apache.maven.plugins:maven-compiler-plugin:3.1:compile

As mentioned above, this means the compile goal from the compiler plugin is bound to the compile phase.

如上所述,这意味着来自compiler插件的compile目标被绑定到compile阶段。

5. Maven Plugin 

5.Maven插件

A Maven plugin is a group of goals; however, these goals aren’t necessarily all bound to the same phase.

一个Maven插件是一组目标;但是,这些目标不一定都绑定在同一个阶段。

For example, here’s a simple configuration of the Maven Failsafe plugin, which is responsible for running integration tests:

例如,这里是Maven Failsafe插件的一个简单配置,它负责运行集成测试。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>${maven.failsafe.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

As we can see, the Failsafe plugin has two main goals configured here:

我们可以看到,Failsafe插件在这里配置了两个主要目标。

  • integration-test: run integration tests
  • verify: verify all integration tests passed

We can use the following command to list all goals in a specific plugin:

我们可以使用以下命令来列出特定插件中的所有目标

mvn <PLUGIN>:help

For example, to list all goals in the Failsafe plugin, we can run:

例如,要列出Failsafe插件中的所有目标,我们可以运行。

mvn failsafe:help

And the output will be:

而输出将是。

This plugin has 3 goals:

failsafe:help
  Display help information on maven-failsafe-plugin.
  Call mvn failsafe:help -Ddetail=true -Dgoal=<goal-name> to display parameter
  details.

failsafe:integration-test
  Run integration tests using Surefire.

failsafe:verify
  Verify integration tests ran using Surefire.

To run a specific goal without executing its entire phase (and the preceding phases), we can use the command:

要运行一个特定的目标而不执行其整个阶段(以及前面的阶段),我们可以使用命令。

mvn <PLUGIN>:<GOAL>

For example, to run the integration-test goal from the Failsafe plugin, we need to run:

例如,为了运行Failsafe插件的integration-test目标,我们需要运行。

mvn failsafe:integration-test

6. Building a Maven Project

6.建立一个Maven项目

To build a Maven project, we need to execute one of the lifecycles by running one of their phases:

要构建一个Maven项目,我们需要通过运行其中的一个阶段来执行一个生命周期。

mvn deploy

This will execute the entire default lifecycle. Alternatively, we can stop at the install phase:

这将执行整个default生命周期。另外,我们也可以在install阶段停止。

mvn install

But usually, we’ll clean the project first by running the clean lifecycle before the new build:

但通常情况下,我们会在新的构建之前先运行clean生命周期来清理该项目:

mvn clean install

We can also run only a specific goal of the plugin:

我们也可以只运行插件的一个特定目标。

mvn compiler:compile

Note that if we try to build a Maven project without specifying a phase or goal, we’ll get an error:

注意,如果我们试图在不指定阶段或目标的情况下构建Maven项目,就会出现错误。

[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal

7. Conclusion

7.结论

In this article, we discussed Maven build lifecycles, as well as the relation between Maven phases and goals.

在这篇文章中,我们讨论了Maven构建生命周期,以及Maven阶段和目标之间的关系。