Before we start

A Java project builds on a laptop but fails in CI, the system that automatically builds and tests code changes, because the tool versions differ. Maven runs dependency retrieval, compilation, tests and packaging from the project definition in its POM file. The JDK is the Java development toolset, and its version matters alongside Maven plugins. This guide follows installation and the first build through alignment between the workstation and CI; installing a newer mvn alone does not guarantee reproducible builds.

As checked on 14 October 2026, the official download page recommends Maven 3.10.0. We use its binary archive to install that exact release. For a later version, check the official download page and adjust the version below.

APT, official binary or Maven Wrapper?

Choose one active executable path and inspect it before building
Method Version authority Updates and reproducibility Best fit
Ubuntu APT Ubuntu Noble package candidate; inspect apt-cache policy OS-managed updates; distribution version can lag upstream Managed workstations when distro version satisfies the project
Official binary Administrator pins archive/checksum; this guide uses 3.10.0 Parallel /opt versions and reviewed symlink change; manual updates Tooling/bootstrap or centrally approved version
Maven Wrapper Project commits wrapper scripts/properties and distribution URL/checksum Same selected Maven across dev/CI; JDK/plugins/dependencies still need control Project CI and teams; bootstrap needs trusted download access
BASH
# Alternative to the /opt installation below, not an extra requirement
apt-cache policy maven
sudo apt install -y maven
mvn -version

Install Java

Start with OpenJDK 21, available in Ubuntu 24.04. If your project requires a different JDK, use the version specified by the project.

BASH
sudo apt update
sudo apt install -y openjdk-21-jdk-headless curl ca-certificates
java -version
javac -version

Both version commands should report 21. If several JDKs are installed, make sure java and javac point to the intended version before continuing.

Maven 3.10.0 runs on JDK 8 or newer; Java 21 here is a selected project toolchain, not its minimum. java and javac must agree. The Maven compiler release and plugin requirements determine target compatibility; changing JAVA_HOME alone does not guarantee that generated bytecode or APIs work on an older runtime. Use Maven toolchains for builds that need a different compiler JDK.

Purpose: Supply the selected JDK before invoking Maven.

Expected result: java and javac both report the intended Java 21 selection.

Verification: Inspect java -version, javac -version and later mvn -version Java home.

Failure analysis: Multiple alternatives can select different runtime and compiler binaries.

Rollback: Restore recorded Java alternatives; retain JDKs required by other projects.

Download Maven

To control the installed version independently of Ubuntu's Maven package, download Apache's archive. Fetch both the archive and the SHA-512 file for that exact version into the download directory. The next step checks the file before extraction.

BASH
mkdir -p ~/maven-install
cd ~/maven-install
MAVEN_VERSION=3.10.0
curl -fLO "https://dlcdn.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz"
curl -fLO "https://downloads.apache.org/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512"
printf '%s  %s\n' "$(cat apache-maven-${MAVEN_VERSION}-bin.tar.gz.sha512)" "apache-maven-${MAVEN_VERSION}-bin.tar.gz" | sha512sum -c -

Continue only when the checksum check prints OK. A mismatch means you should download the files again. If the URL returns 404, check the official download page for the current release.

Purpose: verify the exact archive before privileged extraction. Expected result: sha512sum reports OK for the downloaded file. Failure analysis: stop on a mismatch or HTML error page; verify archive listing before extraction. A checksum fetched from the same compromised source cannot independently prove authenticity; verify Apache’s detached signature against a trusted signing-key fingerprint for a stronger provenance check. Use the official archive location if a superseded release leaves the mirror.

Extract the archive

BASH
# Record the old target; retain the version directory for rollback
if [ -L /opt/maven ]; then readlink /opt/maven | tee ~/maven-install/previous-target.txt; fi
if [ -f /etc/profile.d/maven.sh ]; then sudo cp -a /etc/profile.d/maven.sh /etc/profile.d/maven.sh.before-modernization; fi
BASH
sudo tar -xzf "apache-maven-${MAVEN_VERSION}-bin.tar.gz" -C /opt
sudo ln -sfnT "/opt/apache-maven-${MAVEN_VERSION}" /opt/maven

Maven lives in /opt, and /opt/maven points to the installed version. If /opt/maven is already a real directory from an earlier installation, inspect it first; do not delete it to make this command work.

Expected result: /opt/apache-maven-3.10.0 is root-owned and /opt/maven points there. Verification: readlink -f /opt/maven and /opt/maven/bin/mvn -version. Failure analysis: a real /opt/maven directory must be reviewed instead of overwritten. Rollback: switch the symlink to the recorded previous target; keep both version directories until dependent builds pass.

Add Maven to PATH

The shell now needs to locate both tools. Derive Java's path from the selected binary and add Maven to PATH so mvn works from any directory. The settings persist for later logins; a login-shell setup must not be assumed to configure a CI process too.

BASH
JAVA_HOME_PATH="$(dirname "$(dirname "$(readlink -f "$(command -v java)")")")"
printf 'export JAVA_HOME="%s"\n' "$JAVA_HOME_PATH" | sudo tee /etc/profile.d/maven.sh > /dev/null
sudo tee -a /etc/profile.d/maven.sh > /dev/null <<'EOF'
export MAVEN_HOME=/opt/maven
export PATH="$MAVEN_HOME/bin:$PATH"
EOF
source /etc/profile.d/maven.sh
mvn -version

Look for Apache Maven 3.10.0 and Java version 21. That confirms the shell can find both tools. Maven is a command-line tool; it does not need a systemd service or an open network port.

BASH
type -a mvn
printf 'JAVA_HOME=%s\n' "$JAVA_HOME"
"$JAVA_HOME/bin/javac" -version

Purpose: select the intended executable and JDK for login shells. Expected result: mvn -version reports Maven 3.10.0 and the intended Java home. Failure analysis: non-login CI/systemd environments do not automatically source /etc/profile.d; set their environment explicitly. Rollback: restore maven.sh.before-modernization if it existed; otherwise remove only this newly created profile and start a fresh shell.

Read the lifecycle before choosing a CI goal

Default lifecycle: validate → compile → test → package → verify → install → deploy
Default lifecycle: validate → compile → test → package → verify → install → deploy — Open the diagram to zoom.

The goals do different jobs. Clean removes previous output in a separate lifecycle; verify runs preceding phases and configured checks without proving that tests exist. Install writes to the local repository, while deploy publishes remotely under credentials and policy. Pin plugin and dependency versions in the POM; snapshots and mutable repositories can still change build results.

Run a build

In an existing Maven project, open the folder containing pom.xml and run the command below as your normal user. Maven downloads dependencies on the first run, so that build can take longer.

BASH
mvn clean verify

BUILD SUCCESS means this project's configured build and checks completed; inspect plugin and test reports too. Dependencies normally remain in ~/.m2/repository. Build as an ordinary user, because sudo can leave project or cache files owned by root and break later runs.

Purpose: Exercise the project lifecycle as the ordinary user.

Expected result: Configured verification passes and intended reports/artifacts appear under target.

Verification: Read plugin/test summaries and compare Maven/JDK versions with CI.

Failure analysis: Check POM, credentials/mirror and compilation target; do not build as root.

Rollback: Restore project/toolchain revisions; clean removes only generated build output.

Put the Maven version in the project

Run the wrapper goal once from the project directory using the trusted installed Maven. Review the generated scripts and .mvn/wrapper/maven-wrapper.properties before committing them in your own project. The command pins the distribution version; also record the generated wrapper/plugin version and add verified distributionSha256Sum (and wrapperSha256Sum if a wrapper JAR is used) using the official Wrapper instructions.

BASH
mvn wrapper:wrapper -Dmaven=3.10.0
./mvnw -version
./mvnw -B -ntp clean verify
YAML
# .gitlab-ci.yml; set JAVA_BUILD_IMAGE to an approved JDK 21 image@sha256:digest
image: $JAVA_BUILD_IMAGE
stages:
  - verify
variables:
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
  key:
    files:
      - pom.xml
      - .mvn/wrapper/maven-wrapper.properties
  paths:
    - .m2/repository/
verify:
  stage: verify
  script:
    - ./mvnw -version
    - ./mvnw -B -ntp clean verify
  artifacts:
    when: always
    reports:
      junit: "**/target/surefire-reports/TEST-*.xml"

Expected result: dev and CI report the same Maven distribution, and CI uses the approved JDK digest. Failure analysis: a cache only saves downloads; it is not a dependency lock or a source of trust. Scope caches to trusted jobs and never include settings credentials. Rollback: revert the wrapper/POM/CI change together in the project and run verify with the previously approved toolchain.

Separate GitLab Runner operation from the GitLab server

Resolve dependencies through a controlled path

POM and settings select repositories; the local cache is checked before remote artifact retrieval
POM and settings select repositories; the local cache is checked before remote artifact retrieval — Open the diagram to zoom.

User settings live in ~/.m2/settings.xml and the default cache in ~/.m2/repository. An organizational mirror controls the path for approved artifacts. Put credentials in protected settings with a matching ID, not in the project POM. The following example contains no credentials; replace the hostname and retain proxy settings only when needed.

XML
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0">
  <mirrors>
    <mirror>
      <id>corporate</id>
      <mirrorOf>*</mirrorOf>
      <url>https://repo.example.com/repository/maven-public/</url>
    </mirror>
  </mirrors>
  <proxies>
    <proxy>
      <id>corporate-http</id>
      <active>true</active>
      <protocol>http</protocol>
      <host>proxy.example.com</host>
      <port>3128</port>
      <nonProxyHosts>localhost|127.0.0.1|*.example.com</nonProxyHosts>
    </proxy>
  </proxies>
</settings>

Verify ./mvnw help:effective-settings in a private terminal and inspect the actual download host during verify; the Wrapper distribution download is a separate bootstrap path and must also reach its trusted URL. For PKIX errors, verify the Java truststore and proxy interception certificate; do not disable TLS validation. For missing artifacts, check coordinates, mirror content and credentials before deleting the entire cache. Rollback: restore the prior settings file and retry one known project.

If something goes wrong

mvn: command not found: source /etc/profile.d/maven.sh again and run command -v mvn. If an older version appears, run type -a mvn to see which installation comes first in PATH.

JAVA_HOME errors: check echo "$JAVA_HOME" and "$JAVA_HOME/bin/java" -version. If you change JDK later, update JAVA_HOME in /etc/profile.d/maven.sh. Missing pom.xml: move into the project folder. Dependency download errors: check internet access and any proxy configured by your organization.

Questions from deployment reviews

Does BUILD SUCCESS prove tests ran?

It proves the configured goals succeeded. Inspect the test plugin summary/reports and ensure skips or empty suites are not hiding missing checks.

Does the Wrapper pin Java too?

No. Pin the CI JDK image or configure an approved toolchain separately; the Wrapper selects Maven.

Official references

Apache Maven downloads and current version

Official installation instructions

Getting started with Maven projects

Wrapper distribution and checksum controls

Maven mirrors, proxies and server settings

Maven lifecycle semantics

Share

Meet AJInfrastructure & DevOps
Loading…