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
javacand tools likejar. 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:
| Version | Released | Notes |
|---|---|---|
| 8 | 2014 | Still found in legacy apps |
| 11 | 2018 | Old LTS, being retired |
| 17 | 2021 | Minimum for Spring Boot 3 and Maven 4 |
| 21 | 2023 | The LTS this course uses |
| 25 | 2025 | Newest 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 -vopenjdk 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"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 mavenThis 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 currentUsing:java: 21.0.9-temmaven: 3.9.16sdk 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/mavenThen set the environment variables in ~/.bashrc (or /etc/profile.d/maven.sh for everyone).
The environment variables
echo $JAVA_HOMEecho $PATHwhich java mvn/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| Variable | What it does |
|---|---|
JAVA_HOME | The JDK Maven runs with. Set it explicitly on build machines. |
PATH | Must include $JAVA_HOME/bin and Maven's bin directory. |
MAVEN_OPTS | JVM options for Maven itself, for example more memory: -Xmx2g. |
MAVEN_ARGS | Extra 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
MAVEN_OPTSchanges the JVM that runs Maven, not your application. Ifmvnitself runs out of memory on a big multi-module build, raise-Xmxhere.
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. .. .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.zipNow use ./mvnw exactly like mvn:
./mvnw -v./mvnw -q packageWhat to commit:
mvnwandmvnw.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:
| File | What it holds | Example |
|---|---|---|
.mvn/maven.config | Default command-line options | --no-transfer-progress |
.mvn/jvm.config | JVM options for Maven | -Xmx2g |
.mvn/extensions.xml | Build extensions | the build cache (lesson 14) |
Look at the multi-module project in the terminal, which already has one:
cat ~/platform/.mvn/maven.config--no-transfer-progressJava 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 21Or keep a lean JDK image and use the project's wrapper:
image: eclipse-temurin:21-jdkscript: - ./mvnw --batch-mode verifyBoth 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 ./mvnwThe 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 -vshows the Java Maven really uses; check it first when builds behave differently.- Install Maven with SDKMAN on laptops and the official archive or a
mavenimage on servers. JAVA_HOMEpicks the JDK;MAVEN_OPTStunes 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