LinuxCommandLibrary

mvn-install

Install project artifact to local repository

TLDR

Compile, test, package, and install the project into the local repository

$ mvn install
copy

Skip tests during installation
$ mvn install [[-D|--define]] skipTests
copy

Force update of dependencies before installing
$ mvn install [[-U|--update-snapshots]]
copy

Skip test compilation and execution
$ mvn install [[-D|--define]] maven.test.skip=true
copy

SYNOPSIS

mvn [options] install [goals...]

Examples:
mvn install
mvn clean install
mvn -DskipTests install

PARAMETERS

-D<property>=<value>
    Defines a system property, often used to control build behavior (e.g., -DskipTests=true to skip running tests).

-P <profileId>
    Activates a specified build profile defined in the pom.xml or settings.xml.

-U, --update-snapshots
    Forces a check for updated releases and snapshots from remote repositories.

-B, --batch-mode
    Runs Maven in non-interactive (batch) mode, useful for automated scripts.

-f, --file <file>
    Specifies an alternate path to the pom.xml file to use.

-o, --offline
    Works offline, only using artifacts available in the local repository.

-X, --debug
    Produces extensive debug output during the build process.

-e, --errors
    Produces execution error messages, useful for troubleshooting.

--settings <file>
    Specifies an alternate path for the user settings file (settings.xml).

clean
    (Not a direct option for install, but a common preceding goal) Cleans the target directory, removing previously built artifacts.

DESCRIPTION

The `mvn install` command is a core goal in the Apache Maven build lifecycle, primarily used to compile, test, package, and then install a project's artifact into the local Maven repository. When executed, Maven first runs all prior lifecycle phases including `validate`, `compile`, `test`, `package`, and `verify`. The `compile` phase compiles the project's source code, `test` runs the unit tests, and `package` bundles the compiled code and resources into a distributable format, typically a JAR, WAR, or EAR file. Finally, the `install` phase copies this packaged artifact along with its POM file into the user's local Maven repository, usually located at `~/.m2/repository`. This makes the artifact available as a dependency for other local Maven projects without needing to be deployed to a remote repository. It's a crucial step for multi-module projects or when developing library components locally that are consumed by other applications on the same machine.

CAVEATS

  • Prerequisites: Requires a Java Development Kit (JDK) and Apache Maven to be installed and configured on the system.
  • POM File: Must be executed in a directory containing a valid pom.xml file or specify one using -f.
  • Local Only: The install goal only places the artifact in the local Maven repository (~/.m2/repository). It does not deploy the artifact to any remote repository or make it accessible to other developers. For remote deployment, use the deploy goal.
  • Build Failures: A build will fail if compilation errors occur, tests fail (unless skipped), or dependency resolution issues arise.
  • Disk Usage: Repeated installations of different versions or snapshots can consume local disk space in the ~/.m2/repository.

MAVEN BUILD LIFECYCLE

The install goal is part of Maven's default build lifecycle. Executing mvn install means that all phases leading up to install (like validate, compile, test, package, and verify) will also be executed sequentially. This ensures that the project is properly built and tested before its artifact is placed into the local repository.

THE LOCAL MAVEN REPOSITORY

The local Maven repository, typically located at ~/.m2/repository on Unix-like systems, serves as a cache for project dependencies and a storage location for locally built artifacts. When mvn install is run, the project's artifact (e.g., JAR, WAR) and its corresponding POM file are copied into this repository, organized by group ID, artifact ID, and version. This allows other local projects to declare this artifact as a dependency without needing to fetch it from remote repositories.

HISTORY

Apache Maven originated in 2002, evolving from the Turbine project, as a tool to simplify the build process. Its primary innovation was the introduction of a "convention over configuration" paradigm and a lifecycle-driven build system. The `install` goal has been a fundamental part of Maven's standard build lifecycle since its early days, designed to facilitate local development by making project artifacts readily available for consumption by other dependent projects on the same machine. This approach standardized how Java projects were built, managed dependencies, and integrated with various tools, significantly improving developer productivity compared to earlier build tools like Ant, which required extensive manual configuration for every project.

SEE ALSO

mvn clean, mvn compile, mvn test, mvn package, mvn deploy, java(1), javac(1)

Copied to clipboard