What is the difference between "mvn deploy" to a local repo and "mvn install"?

Learn what is the difference between "mvn deploy" to a local repo and "mvn install"? with practical examples, diagrams, and best practices. Covers maven-2 development techniques with visual explana...

mvn deploy vs. mvn install: Understanding Maven Artifact Management

mvn deploy vs. mvn install: Understanding Maven Artifact Management

Explore the crucial differences between mvn deploy to a local repository and mvn install in Maven, and learn when to use each command for effective project management.

Maven is a powerful build automation tool widely used in Java projects. Two commands often cause confusion for new users: mvn install and mvn deploy. While both commands place project artifacts into a repository, their intended purposes and target repositories differ significantly. This article will clarify these distinctions, helping you manage your project artifacts more effectively.

Understanding mvn install

The mvn install command compiles your project's source code, runs tests, packages the compiled code into a JAR/WAR file (the artifact), and then places this artifact into your local Maven repository. The local repository is typically located in your user's home directory under .m2/repository. Its primary purpose is to provide local access to artifacts for other projects on your machine that declare a dependency on the installed project. It's a local cache for your machine's Maven builds, making artifacts available for immediate use without requiring network access.

mvn clean install

This command cleans the project, compiles, tests, packages, and installs the artifact to the local Maven repository.

A flowchart diagram illustrating the 'mvn install' process. Start node 'Project Source Code' connects to 'Compile & Test', then to 'Package Artifact (JAR/WAR)', and finally to 'Install to Local Maven Repository'. Arrows indicate flow. Use light blue for processes and a darker blue for the repository.

The mvn install workflow

Understanding mvn deploy

The mvn deploy command, on the other hand, is designed for sharing your project's artifacts with other developers and build servers. It installs the artifact to your local repository first (just like mvn install), and then copies it to a remote repository, often a corporate Maven repository manager like Nexus or Artifactory. This remote repository acts as a central hub for all project artifacts within an organization, facilitating collaboration and consistent dependency management across teams and environments. mvn deploy requires specific configuration in your project's pom.xml (distributionManagement section) and your Maven settings.xml for authentication to the remote repository.

mvn clean deploy

This command cleans, compiles, tests, packages, installs locally, and then deploys the artifact to the configured remote Maven repository.

<project>
    ...
    <distributionManagement>
        <repository>
            <id>central-releases</id>
            <name>Corporate Releases</name>
            <url>http://your-repo-manager/nexus/content/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>central-snapshots</id>
            <name>Corporate Snapshots</name>
            <url>http://your-repo-manager/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>
    ...
</project>

Example pom.xml configuration for deploying artifacts to a remote repository.

A flowchart diagram illustrating the 'mvn deploy' process. 'Project Source Code' connects to 'Compile & Test', then to 'Package Artifact (JAR/WAR)'. From there, it branches: one arrow to 'Install to Local Maven Repository' and another to 'Deploy to Remote Maven Repository'. Arrows indicate flow. Use light blue for processes, a darker blue for local repo, and a vibrant green for the remote repo.

The mvn deploy workflow, including local installation and remote deployment.

Key Differences and Use Cases

The fundamental difference lies in the target repository and the scope of artifact sharing. mvn install is for local development and making artifacts available for personal use on your machine. mvn deploy is for sharing artifacts globally within an organization, enabling continuous integration, and ensuring consistent builds. Here's a summary of their distinct roles:

A comparison table diagram showing 'mvn install' vs 'mvn deploy'. Columns for 'Feature', 'mvn install', and 'mvn deploy'. Rows include: 'Target Repository' (Local vs. Remote), 'Scope' (Personal vs. Team/Organization), 'Purpose' (Local Dependency Resolution vs. Centralized Artifact Management), 'Configuration' (Minimal vs. distributionManagement and settings.xml), and 'Typical Use' (Local Development vs. CI/CD, Releases). Use a clean, two-column layout with distinct colors for each command's characteristics.

Comparison of mvn install and mvn deploy

In summary, use mvn install during your day-to-day development cycle to quickly make changes available to other local projects. Use mvn deploy when your artifact is stable enough to be shared with the wider team or when preparing for a release, pushing it to a central repository where all team members and automated systems can access it. Understanding these commands is crucial for efficient Maven project management and healthy CI/CD pipelines.