What is the difference between "mvn deploy" to a local repo and "mvn install"?
Categories:
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.
The mvn install
workflow
mvn install
as making your project's artifact available for your other local projects to consume as a dependency. It's a localized operation.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.
The mvn deploy
workflow, including local installation and remote deployment.
file://
URL in distributionManagement
) is generally discouraged. It bypasses the benefits of a central repository and can lead to inconsistent builds and dependency management issues across different developer machines.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:
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.