Skip to content
$ lynuxpro
← Maven & Build Tools

LESSON 2 OF 16 · BEGINNER · 15 MIN

Installing Java and Maven

Free previewLesson 2 of the 2 free lessons in this Pro course.Unlock all 16

HOW IT WORKSWhat a Maven build needs on a machine

runs onJDK 21JAVA_HOME./mvnwcommitted in the repoMaven 3.9.16on the PATH~/.m2cache and settingsmaven-wrapper.propertiespinned versionYour projectpom.xml
1/5

Maven is a Java program, so it needs a JDK. JAVA_HOME decides which one.

$ mvn -v
Apache Maven 3.9.16
Java version: 21.0.9, vendor: Eclipse Adoptium

Before Maven can build anything it needs a Java Development Kit, and every machine that builds the project (your laptop, teammates' laptops, CI runners, Docker images) should use the same versions. This lesson covers which Java to install, three ways to install Maven, the environment variables that tie them together, and the Maven Wrapper, which pins the Maven version inside the project so nobody has to install it at all.

In simple words

  • JDK: the Java Development Kit: everything needed to compile and run Java.
  • JAVA_HOME: an environment variable pointing to the JDK, so tools know which Java to use.
  • SDKMAN: a tool for installing and switching between Java and Maven versions on a laptop.
  • Maven Wrapper (mvnw): a small script in the project that downloads the exact Maven version, so every machine builds the same way.

JDK, JRE and versions

  • The JRE (Java Runtime Environment) only runs Java programs.
  • The JDK (Java Development Kit) adds the compiler javac and tools like jar. Builds need a JDK.

Java ships a new release every six months, and every two years one is a Long-Term Support (LTS) release that gets security fixes for years. Production systems stick to LTS versions:

VersionReleasedNotes
82014Still found in legacy apps
112018Old LTS, being retired
172021Minimum for Spring Boot 3 and Maven 4
212023The LTS this course uses
252025Newest LTS

There are several free, compatible builds of the same OpenJDK code: Eclipse Temurin (Adoptium), Amazon Corretto, Azul Zulu, Microsoft Build of OpenJDK and Oracle's OpenJDK builds. Pick one per organisation and use it everywhere. Temurin is a safe default and is what the official maven Docker images use.

Check what is installed

java -versionjavac -versionmvn -v
SHELLbashutf-8 · 3L
openjdk version "21.0.9" 2025-10-21 LTSOpenJDK Runtime Environment Temurin-21.0.9+10 (build 21.0.9+10-LTS)OpenJDK 64-Bit Server VM Temurin-21.0.9+10 (build 21.0.9+10-LTS, mixed mode, sharing)javac 21.0.9Apache Maven 3.9.16 (6a8fbb1b0f5d0c66f7d5a8a6a8e4b1d0e2f3c4a5)Maven home: /opt/mavenJava version: 21.0.9, vendor: Eclipse Adoptium, runtime: /usr/lib/jvm/temurin-21-jdk-amd64Default locale: en_IN, platform encoding: UTF-8OS name: "linux", version: "6.8.0-49-generic", arch: "amd64", family: "unix"
OUTPUTterminalutf-8 · 9L

mvn -v is the most useful of the three because it shows which Java Maven itself will use. If java -version says 21 but mvn -v says 17, Maven is picking up a different JDK through JAVA_HOME, a classic cause of "it compiles on my machine".

Option 1: the package manager

On Ubuntu or Debian:

sudo apt install -y openjdk-21-jdk maven
SHELLbashutf-8 · 1L

This is quick, but distributions often ship a Maven version that is a year or more old. Fine for a CI image you rebuild often; less good if you need a specific version.

Option 2: SDKMAN (best for laptops)

SDKMAN installs and switches between many versions of Java, Maven and Gradle per user, with no sudo:

sdk list javasdk install java 21.0.9-temsdk install maven 3.9.16sdk current
SHELLbashutf-8 · 4L
Using:java: 21.0.9-temmaven: 3.9.16
OUTPUTterminalutf-8 · 4L

sdk use java 17.0.17-tem switches the current shell to another JDK, handy when you support projects on different Java versions.

Option 3: download and unpack (servers and Docker images)

The official way is to download the binary archive from maven.apache.org, verify its checksum, unpack it and put its bin directory on the PATH:

curl -fsSLO https://archive.apache.org/dist/maven/maven-3/3.9.16/binaries/apache-maven-3.9.16-bin.tar.gzcurl -fsSLO https://archive.apache.org/dist/maven/maven-3/3.9.16/binaries/apache-maven-3.9.16-bin.tar.gz.sha512echo "$(cat apache-maven-3.9.16-bin.tar.gz.sha512)  apache-maven-3.9.16-bin.tar.gz" | sha512sum -c -sudo tar -xzf apache-maven-3.9.16-bin.tar.gz -C /optsudo ln -sfn /opt/apache-maven-3.9.16 /opt/maven
SHELLbashutf-8 · 5L

Then set the environment variables in ~/.bashrc (or /etc/profile.d/maven.sh for everyone).

The environment variables

echo $JAVA_HOMEecho $PATHwhich java mvn
SHELLbashutf-8 · 3L
/usr/lib/jvm/temurin-21-jdk-amd64/opt/maven/bin:/usr/lib/jvm/temurin-21-jdk-amd64/bin:/usr/local/bin:/usr/bin:/bin/usr/bin/java/usr/bin/mvn
OUTPUTterminalutf-8 · 4L
VariableWhat it does
JAVA_HOMEThe JDK Maven runs with. Set it explicitly on build machines.
PATHMust include $JAVA_HOME/bin and Maven's bin directory.
MAVEN_OPTSJVM options for Maven itself, for example more memory: -Xmx2g.
MAVEN_ARGSExtra arguments added to every mvn command (Maven 3.9+).

Set them for the session like this:

export JAVA_HOME=/usr/lib/jvm/temurin-21-jdk-amd64export MAVEN_OPTS="-Xmx1g -XX:+UseParallelGC"mvn -v
SHELLbashutf-8 · 3L

MAVEN_OPTS changes the JVM that runs Maven, not your application. If mvn itself runs out of memory on a big multi-module build, raise -Xmx here.

The Maven Wrapper: pin Maven inside the project

Installing Maven on every machine causes version drift: one runner has 3.8, a laptop has 3.9.16, a new hire has whatever apt gave them. The Maven Wrapper fixes this. You generate a small script, mvnw, and commit it with the project. The first time anyone runs ./mvnw, it downloads the exact Maven version written in the project, then uses it from then on.

Generate it in the shop-api project:

cd ~/shop-apimvn -q wrapper:wrapper -Dmaven=3.9.16ls -acat .mvn/wrapper/maven-wrapper.properties
SHELLbashutf-8 · 4L
.  ..  .gitlab-ci.yml  .mvn  ci  mvnw  mvnw.cmd  pom.xml  srcwrapperVersion=3.3.2distributionType=only-scriptdistributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.16/apache-maven-3.9.16-bin.zip
OUTPUTterminalutf-8 · 4L

Now use ./mvnw exactly like mvn:

./mvnw -v./mvnw -q package
SHELLbashutf-8 · 2L

What to commit:

  • mvnw and mvnw.cmd (the Linux/macOS and Windows scripts)
  • .mvn/wrapper/maven-wrapper.properties (which Maven version to use)

To upgrade Maven for the whole team, change the version in that one properties file (or re-run mvn wrapper:wrapper -Dmaven=NEW) and commit. CI picks it up on the next run. In corporate networks, point distributionUrl at your Artifactory or Nexus so runners never need internet access.

The .mvn directory

Besides the wrapper, the .mvn folder in a project's root can hold settings that apply to everyone who builds it:

FileWhat it holdsExample
.mvn/maven.configDefault command-line options--no-transfer-progress
.mvn/jvm.configJVM options for Maven-Xmx2g
.mvn/extensions.xmlBuild extensionsthe build cache (lesson 14)

Look at the multi-module project in the terminal, which already has one:

cat ~/platform/.mvn/maven.config
SHELLbashutf-8 · 1L
--no-transfer-progress
OUTPUTterminalutf-8 · 1L

Java and Maven in Docker and CI

In CI you rarely install anything. Use an image that has both, and pin its tag:

image: maven:3.9-eclipse-temurin-21     # Maven 3.9.x on Temurin JDK 21
NORMALyamlutf-8 · 1L

Or keep a lean JDK image and use the project's wrapper:

image: eclipse-temurin:21-jdkscript:  - ./mvnw --batch-mode verify
NORMALyamlutf-8 · 3L

Both are fine. The second has one source of truth for the Maven version (the wrapper), so local and CI builds always match.

Where Maven keeps things

~/.m2/├── repository/      downloaded and installed artifacts (the local repository)├── settings.xml     your user settings: mirrors, credentials, profiles (optional)└── wrapper/         Maven versions downloaded by ./mvnw
OUTPUTterminalutf-8 · 4L

The local repository grows over time. Deleting it is safe: Maven downloads again what it needs, which is also a good way to prove a build doesn't depend on something only you have locally.

Key points

  • Builds need a JDK; use an LTS release (21 here) from one distribution everywhere.
  • mvn -v shows the Java Maven really uses; check it first when builds behave differently.
  • Install Maven with SDKMAN on laptops and the official archive or a maven image on servers.
  • JAVA_HOME picks the JDK; MAVEN_OPTS tunes Maven's own JVM.
  • Commit the Maven Wrapper (mvnw, .mvn/wrapper/) so every machine uses the same Maven version.

You've finished the free preview of Maven & Build Tools

Upgrade to Pro to unlock the remaining 14 lessons, every practice lab and a certificate when you finish.

See plans