TExcelTAD
Home › Java Builds: Maven & Gradle › install & deploy

install & deploy

The last two phases of the default lifecycle copy your artifact somewhere other builds can find it. install copies it into the local repository on your machine; deploy uploads it to a remote repository that your team or the world uses. Both write the same Maven repository layout, which Gradle, sbt, Bazel and every repository manager understand.

The repository layout

An artifact's coordinates map to a path: dots in the groupId become folders, then artifactId, then version, then the file artifactId-version[-classifier].extension:

python · try it yourself
def path(coords, ext="jar", classifier=None):
    group, artifact, version = coords.split(":")
    name = f"{artifact}-{version}" + (f"-{classifier}" if classifier else "") + f".{ext}"
    return "/".join(group.split(".") + [artifact, version, name])

print(path("org.apache.commons:commons-lang3:3.21.0"))
print(path("org.apache.commons:commons-lang3:3.21.0", "pom"))
print(path("com.fasterxml.jackson.core:jackson-databind:2.22.4", classifier="sources"))
print(path("org.junit:junit-bom:5.14.4", "pom"))
org/apache/commons/commons-lang3/3.21.0/commons-lang3-3.21.0.jar
org/apache/commons/commons-lang3/3.21.0/commons-lang3-3.21.0.pom
com/fasterxml/jackson/core/jackson-databind/2.22.4/jackson-databind-2.22.4-sources.jar
org/junit/junit-bom/5.14.4/junit-bom-5.14.4.pom

A classifier (sources, javadoc, tests, linux-x86_64) attaches extra files to the same version. Every artifact has a .pom next to its JAR: that's how consumers learn its dependencies.

install

bash · try it yourself
mkdir -p greet/src/main/java/acme && cd greet
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.acme</groupId>
  <artifactId>greet</artifactId>
  <version>1.0.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>
POM
echo 'package acme; public class Greet { public static String hi(String n) { return "Hi " + n; } }' > src/main/java/acme/Greet.java
mvn install | grep -E -- '--- install|Installing' | sed "s#$HOME#~#g"
find ~/.m2/repository/org/acme -type f | sed "s#$HOME#~#" | sort
[INFO] --- install:3.1.4:install (default-install) @ greet ---
[INFO] Installing ~/greet/pom.xml to ~/.m2/repository/org/acme/greet/1.0.0/greet-1.0.0.pom
[INFO] Installing ~/greet/target/greet-1.0.0.jar to ~/.m2/repository/org/acme/greet/1.0.0/greet-1.0.0.jar
~/.m2/repository/org/acme/greet/1.0.0/_remote.repositories
~/.m2/repository/org/acme/greet/1.0.0/greet-1.0.0.jar
~/.m2/repository/org/acme/greet/1.0.0/greet-1.0.0.pom
~/.m2/repository/org/acme/greet/maven-metadata-local.xml

Besides the JAR and POM, install writes two bookkeeping files: maven-metadata-local.xml lists the versions installed locally, and _remote.repositories records where each file came from (here: nowhere, it was built locally).

Anything in ~/.m2/repository is visible to every Maven build on the machine. That's convenient for a library you're testing in another project, and dangerous everywhere else: an installed snapshot quietly replaces what the team repository would have served. Use install deliberately, and use verify in CI.

deploy

deploy needs to know where to upload. The POM says so in <distributionManagement>, with separate repositories for releases and snapshots; credentials come from a <server> with the same id in settings.xml:

bash · try it yourself
mkdir -p greet/src/main/java/acme && cd greet
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.acme</groupId>
  <artifactId>greet</artifactId>
  <version>1.0.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.outputTimestamp>2026-01-01T00:00:00Z</project.build.outputTimestamp>
  </properties>
</project>
POM
echo 'package acme; public class Greet { public static String hi(String n) { return "Hi " + n; } }' > src/main/java/acme/Greet.java
echo "--- no distributionManagement"
mvn deploy | grep -o 'Deployment failed: .*'
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.acme</groupId>
  <artifactId>greet</artifactId>
  <version>1.0.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.outputTimestamp>2026-01-01T00:00:00Z</project.build.outputTimestamp>
  </properties>
  <distributionManagement>
    <repository><id>company</id><url>file:///srv/maven-repo/releases</url></repository>
    <snapshotRepository><id>company</id><url>file:///srv/maven-repo/snapshots</url></snapshotRepository>
  </distributionManagement>
</project>
POM
echo "--- offline"
mvn -o deploy | grep -o 'Cannot deploy.*'
echo "--- deploy"
mvn -B deploy | grep -E 'Uploading|BUILD'
find /srv/maven-repo -type f | sort
grep -v lastUpdated /srv/maven-repo/releases/org/acme/greet/maven-metadata.xml
--- no distributionManagement
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::url parameter -> [Help 1]
--- offline
Cannot deploy artifacts when Maven is in offline mode -> [Help 1]
--- deploy
[INFO] Uploading to company: file:///srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.pom
[INFO] Uploading to company: file:///srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.jar
[INFO] Uploading to company: file:///srv/maven-repo/releases/org/acme/greet/maven-metadata.xml
[INFO] BUILD SUCCESS
/srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.jar
/srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.jar.md5
/srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.jar.sha1
/srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.pom
/srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.pom.md5
/srv/maven-repo/releases/org/acme/greet/1.0.0/greet-1.0.0.pom.sha1
/srv/maven-repo/releases/org/acme/greet/maven-metadata.xml
/srv/maven-repo/releases/org/acme/greet/maven-metadata.xml.md5
/srv/maven-repo/releases/org/acme/greet/maven-metadata.xml.sha1
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>org.acme</groupId>
  <artifactId>greet</artifactId>
  <versioning>
    <release>1.0.0</release>
    <versions>
      <version>1.0.0</version>
    </versions>
  </versioning>
</metadata>
  • Without distributionManagement, deploy stops and tells you exactly what's missing. -DaltDeploymentRepository=id::url is the command-line alternative (handy in CI).
  • mvn -o deploy is refused: uploading is a network operation, even when the "network" is a folder.
  • Each file is uploaded with .md5 and .sha1 checksums, and maven-metadata.xml at the artifact level lists the versions; consumers use it to resolve version ranges and LATEST.

SNAPSHOT deploys

A -SNAPSHOT version goes to the snapshot repository, and each deploy gets a unique timestamped name; maven-metadata.xml in the version folder points at the newest. Consumers asking for 1.1.0-SNAPSHOT get the latest build. (The session masks the timestamp, which is the real clock.)

bash · try it yourself
mkdir -p greet/src/main/java/acme && cd greet
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.acme</groupId>
  <artifactId>greet</artifactId>
  <version>1.1.0-SNAPSHOT</version>
  <properties><maven.compiler.release>21</maven.compiler.release></properties>
  <distributionManagement>
    <repository><id>company</id><url>file:///srv/maven-repo/releases</url></repository>
    <snapshotRepository><id>company</id><url>file:///srv/maven-repo/snapshots</url></snapshotRepository>
  </distributionManagement>
</project>
POM
echo 'package acme; public class Greet { }' > src/main/java/acme/Greet.java
mvn -q -ntp deploy
mvn -q -ntp deploy
find /srv/maven-repo/snapshots -name '*.jar' | sort | sed -E 's/[0-9]{8}\.[0-9]{6}/<TIMESTAMP>/'
grep -E '<buildNumber>' /srv/maven-repo/snapshots/org/acme/greet/1.1.0-SNAPSHOT/maven-metadata.xml
/srv/maven-repo/snapshots/org/acme/greet/1.1.0-SNAPSHOT/greet-1.1.0-<TIMESTAMP>-1.jar
/srv/maven-repo/snapshots/org/acme/greet/1.1.0-SNAPSHOT/greet-1.1.0-<TIMESTAMP>-2.jar
      <buildNumber>2</buildNumber>

Note: Release versions are immutable. Repository managers reject a second upload of greet-1.0.0.jar (a plain folder like this one doesn't check). If a release is broken, publish 1.0.1; never replace 1.0.0, because someone has already cached it.

Test yourself

Where does the JAR of com.acme.tools:cli:2.1.0 live in a Maven repository?

  1. com.acme.tools/cli/2.1.0/cli.jar
  2. com/acme/tools/cli/2.1.0/cli-2.1.0.jar
  3. cli/2.1.0/com-acme-tools-cli.jar
  4. com/acme/tools/cli-2.1.0.jar

Why is mvn install usually the wrong command in CI?

  1. It's slower than package
  2. It copies artifacts into the machine's local repository, where they can leak into later builds; verify runs every check without that
  3. It deploys to production
  4. It skips tests

What happens when you deploy 1.1.0-SNAPSHOT twice?

  1. The second deploy fails
  2. Each deploy gets a unique timestamped file name and the metadata points to the latest build
  3. The JAR is overwritten in place
  4. Maven renames it to 1.1.1