Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

What is the difference between mvn install and mvn deploy?

IntermediateConceptJava builds (Maven, Gradle, JAR, WAR)
Answer

Short answer

Both build the artifact. mvn install then copies it into your local repository (~/.m2/repository) so other projects on the same machine can use it. mvn deploy goes one step further and uploads it to a remote repository (Nexus/Artifactory) so the whole team and other pipelines can use it.

The lifecycle order

validate → compile → test → package → verify → install → deploy
                                                 ↑ local      ↑ remote

Running deploy also runs install and everything before it.

Where deploy uploads to

<distributionManagement>
  <repository>
    <id>nexus</id>
    <url>https://nexus.company.com/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus</id>
    <url>https://nexus.company.com/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

The id must match a <server> entry in settings.xml that holds the credentials.

Common deploy error

401 Unauthorized → wrong or missing credentials for that server id. 400 Bad Request on a release repository → that version already exists and redeploy is disabled — bump the version.

What to say in the interview

"install puts the artifact in the local ~/.m2 cache; deploy uploads it to Nexus using distributionManagement and the credentials from settings.xml. In CI we usually run mvn deploy only on the main branch or on release tags."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR

More on Java builds (Maven, Gradle, JAR, WAR)