Difference Between mvn install and mvn verify – mvn install 与 mvn verify 的区别

最后修改: 2024年 1月 24日

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

1. Introduction

1.导言

Apache Maven is a powerful build management tool that provides a structured way to manage the build lifecycle of a project. Maven builds are composed of lifecycles, which define clearly how a project is built and distributed.

Apache Maven 是一款功能强大的构建管理工具,它提供了一种管理项目构建生命周期的结构化方法。Maven 构建由生命周期组成,生命周期明确定义了项目的构建和发布方式。

Two very useful commands that play a key role in the build process are mvn install and mvn verify. In this tutorial, we’ll compare and contrast these two commands, understanding the differences between them.

在构建过程中起关键作用的两个非常有用的命令是 mvn installmvn verify。在本教程中,我们将对比这两个命令,了解它们之间的区别。

2. Maven Lifecycles

2.Maven 生命周期

Maven defines three standard lifecycles — clean, default, and site — where each one has a distinct purpose:

Maven 定义了三个标准生命周期–清洁、默认、网站–其中每个生命周期都有不同的目的:

  • The clean lifecycle is responsible for cleaning the project.
  • The default lifecycle is for building and deploying.
  • The site lifecycle is for creating the project’s site documentation.

Each lifecycle consists of phases, and each phase represents a stage in the lifecycle.

每个生命周期由阶段组成,每个阶段代表生命周期中的一个阶段。

2.1. Maven’s default Lifecycle

2.1.Maven 的 default 生命周期

The default lifecycle handles the build process, starting from project compilation and ending with the deployment of artifacts. It is composed of 23 phases and below are the six major phases:

默认生命周期处理构建流程,从项目编译开始,到部署工件结束。它由 23 个阶段组成,以下是六个主要阶段:

  1. validate: The project is validated to ensure that all necessary information is available for the build.
  2. compile: The source code is compiled into bytecode.
  3. test: Unit tests are executed to ensure the code’s correctness.
  4. package: The compiled code is packaged into a JAR or WAR file, depending on the project type.
  5. verify: Additional verification checks run, typically integration tests or ones specified by plugins.
  6. install: The packaged artifact is installed into the local Maven repository, ~/.m2/repository, making it available for other projects on the same machine.

When running a command, we only need to call the last build phase we want to be executed. For instance, running mvn test will execute, in order, the validate, compile, and test phases.

运行命令时,我们只需调用希望执行的最后一个构建阶段。例如,运行 mvn test 将依次执行 验证编译 测试 阶段。

3. Setup

3.设置

In our pom.xml, let’s import a dependency for the maven-surefire-plugin that’s required to run unit tests during the test phase:

pom.xml中,让我们导入依赖maven-surefire-plugin,它是在测试阶段运行单元测试所必需的:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
</plugin>

Let’s also import and configure the dependency configure the maven-failsafe-plugin, binding the integration-test goal to the integration-test phase and the verify goal to the verify phase:

我们还将导入并配置 依赖关系配置 maven-failsafe-plugin ,将 integration-test 目标绑定到 integration-test 阶段,将 verify 目标绑定到 verify 阶段:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.1.2</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Next, let’s write a simple Java program that returns a String when executed:

接下来,让我们编写一个简单的 Java 程序,执行后返回 String 字符串:

class CourseApp {
    String getCourse() {
        return "Baeldung Spring Masterclass";
    }
}

We’ll define a unit test that will be executed as part of the test phase:

我们将定义一个单元测试,作为 test 阶段的一部分来执行:

@Test
void whenGetCourse_ThenCourseShouldBePresent() {
    CourseApp courseApp = new CourseApp();
    
    assertEquals("Baeldung Spring Masterclass", courseApp.getCourse());
}

Similarly, a simple integration test will be executed as part of the integration-test goal of the verify phase.

同样,作为验证阶段集成测试目标的一部分,将执行一个简单的集成测试。

@Test
void givenIntegrationTest_whenGetCourse_ThenCourseShouldBePresent() {
    CourseApp courseApp = new CourseApp();
    
    assertEquals("Baeldung Spring Masterclass", courseApp.getCourse());
} class="language-xml">

4. Maven Verify Phase

4.Maven 验证阶段

The mvn verify command triggers five phases: validate, compile, test, package, and verify. The verify phase is intended to be used for verifying a project’s integrity and quality.

mvn verify 命令会触发五个阶段:验证编译测试打包验证verify 阶段用于验证项目的完整性和质量。

Since we bound the verify phase to the integration-test goal using the maven-failsafe-plugin, our integration tests will run. Maven considers the project verified only if both unit and integration tests pass:

由于我们使用 maven-failafe-plugin 将验证阶段与 integration-test 目标绑定,因此集成测试将会运行。只有当单元测试和集成测试都通过时,Maven 才会认为项目已验证

Maven Verify Phase

By executing the verify phase, we can ensure that our project undergoes thorough testing, including integration testing, before being packaged into an artifact and distributed. This phase can help us maintain the reliability and quality of our application.

通过执行验证阶段,我们可以确保项目在打包成构件并发布之前经过全面的测试,包括集成测试。这一阶段可以帮助我们保持应用程序的可靠性和质量。

5. Maven Install Phase

5. maven 安装阶段

On the other hand, mvn install is responsible for installing the project’s artifacts into the local repository. It triggers the install phase, which comes directly after verify. Thus, it serves the double purpose of verifying our code’s quality and installing our artifact locally.

另一方面,mvn install 负责将项目的工件安装到本地版本库中。它会触发 install 阶段,该阶段直接位于 verify 之后。因此,它具有双重目的:验证代码的质量和在本地安装工件

When we run mvn install, Maven executes the install phase in addition to all the previous steps of the verify phase:

当我们运行 mvn install 时,Maven 除了执行 install 阶段外,还会执行 verify 阶段的所有先前步骤:

Maven install phase

This is particularly useful when working on multiple projects that have dependencies on one another, as it avoids having to copy JAR files between projects manually.

这在处理相互依赖的多个项目时特别有用,因为它避免了在项目之间手动复制 JAR 文件。

6. Differences Between mvn install and mvn verify

6.mvn installmvn verify 之间的区别

While both mvn install and mvn verify contribute to the default lifecycle and share common phases, they serve slightly different purposes:

虽然 mvn installmvn verify 都有助于默认生命周期并共享共同的阶段,但它们的目的略有不同:

  • mvn verify focuses on performing quality checks beyond simple unit testing, such as integration tests and custom checks configured through plugins. It is recommended when the objective is to ensure the project meets specific quality criteria.
  • mvn install is primarily used for installing the project’s artifacts into the local repository. It’s often used during development to share artifacts between projects on the same machine.

7. Conclusion

7.结论

Understanding the different Maven lifecycles and phases is essential for effective project management and collaboration in a Maven-powered build environment.

了解不同的 Maven 生命周期和阶段对于在 Maven 驱动的构建环境中进行有效的项目管理和协作至关重要。

In this article, we discussed how mvn install is the go-to command for local development and dependency management. In contrast, we’ve seen how mvn verify can be used to perform integrity and quality checks against our project.

在本文中,我们讨论了 mvn install 如何成为本地开发和依赖关系管理的首选命令。相比之下,我们已经看到 mvn verify 如何用于对我们的项目执行完整性和质量检查。

As always, the code is available over on GitHub.

与往常一样,代码可在 GitHub 上获取。