Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

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.

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.

2. Maven Lifecycles

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

  • 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

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:

  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.

3. Setup

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:

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

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

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

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.

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

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.

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

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.

6. Differences Between mvn install and mvn verify

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

  • 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

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

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.

As always, the code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Maven (eBook) (cat=Maven)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.