What does mvn install in maven exactly do

Learn what does mvn install in maven exactly do with practical examples, diagrams, and best practices. Covers java, maven, pom.xml development techniques with visual explanations.

Understanding 'mvn install': What Maven Does Under the Hood

Hero image for What does mvn install in maven exactly do

Explore the 'mvn install' command in Maven, its lifecycle phases, and how it builds, tests, and places your project artifacts into the local repository for reuse.

The mvn install command is one of the most frequently used Maven commands, yet its exact implications can sometimes be misunderstood. It's a powerful command that orchestrates a series of build lifecycle phases, culminating in the placement of your project's compiled artifact into your local Maven repository. This article will break down what mvn install does, its key phases, and why it's crucial for Maven-based development.

The Maven Build Lifecycle

Before diving into install, it's essential to understand Maven's build lifecycle. Maven defines a standard set of phases that can be executed. When you invoke a command like mvn install, Maven executes all phases up to and including the specified phase in the default lifecycle. The default lifecycle includes phases such as validate, compile, test, package, verify, and install.

flowchart TD
    A[Start mvn install] --> B(validate)
    B --> C(compile)
    C --> D(test)
    D --> E(package)
    E --> F(verify)
    F --> G(install)
    G --> H[End mvn install]

    subgraph Default Lifecycle Phases
        B --> C
        C --> D
        D --> E
        E --> F
        F --> G
    end

Simplified Maven Default Lifecycle Phases executed by mvn install

Key Phases Executed by mvn install

When you run mvn install, Maven executes the following phases in sequence, assuming no errors occur:

  1. validate: Checks if the project is correct and all necessary information is available.
  2. compile: Compiles the source code of the project.
  3. test: Runs tests against the compiled source code. These are typically unit tests.
  4. package: Takes the compiled code and packages it in its distributable format, such as a JAR or WAR file.
  5. verify: Runs checks to validate the integrity of the package. This often includes running integration tests and checking for quality gates.
  6. install: Installs the package into the local repository, for use as a dependency in other projects locally.
mvn install

Executing the mvn install command

The Local Repository and Artifacts

The most significant outcome of mvn install is the placement of your project's artifact (e.g., my-app-1.0.jar) into your local Maven repository. This repository is typically located at ~/.m2/repository on Unix-like systems or C:\Users\YourUser\.m2\repository on Windows. Once an artifact is in the local repository, any other Maven project on your machine can declare it as a dependency and use it without needing to build it from source or fetch it from a remote repository.

When to Use mvn install vs. mvn package

While mvn install is very common, it's important to differentiate it from mvn package.

  • mvn package: Builds your project and creates the artifact (e.g., JAR, WAR) in the target directory. It does not place the artifact into your local repository.
  • mvn install: Does everything mvn package does, and then additionally copies the artifact to your local Maven repository.

Use mvn package when you just want to build the artifact for immediate use (e.g., deploying to a server) and don't need it as a dependency for other local projects. Use mvn install when you want to make your project's artifact available to other local projects as a dependency.

<!-- Example pom.xml snippet declaring a dependency -->
<dependencies>
    <dependency>
        <groupId>com.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

A pom.xml declaring a dependency that would be resolved from the local repository after mvn install