TP1.2 — Installing Maven 3.9.x
Objectives
This practical work will allow you to:
- Install Apache Maven 3.9.x on your operating system
- Configure the necessary environment variables
- Verify that the installation works correctly
- Understand the role of Maven in a Spring Boot project
Estimated duration: 30-45 minutes
Why Maven?
Apache Maven is the standard build tool for Java and Spring Boot projects. It manages:
- Dependencies: automatic downloading of necessary libraries
- Build: compilation, tests, application packaging
- Lifecycle: clean → compile → test → package → install → deploy
Your Java code ──► Maven ──► Ready application (.jar)
│
├── Downloads dependencies
├── Compiles code
├── Runs tests
└── Creates the .jar file
We use Maven 3.9.x for its optimal compatibility with Java 17+ and Spring Boot 3.x.
Prerequisites
Before installing Maven, you must have Java JDK 17 or higher installed.
Verify Java
java -version
Expected result (example):
openjdk version "17.0.x" 2024-xx-xx
OpenJDK Runtime Environment ...
OpenJDK 64-Bit Server VM ...
If Java is not installed, check our detailed guide:
Or download it directly from:
- Eclipse Adoptium (Temurin) (recommended)
- Oracle JDK
Installation on Windows
Step 1: Download Maven
- Go to https://maven.apache.org/download.cgi
- Download the Binary zip archive file:
apache-maven-3.9.9-bin.zip
If version 3.9.9 is no longer available, take the latest 3.9.x version from the download page.
Step 2: Extract the archive
- Create a folder
C:\Program Files\Maven(orC:\maven) - Extract the ZIP content into this folder
- You should have:
C:\Program Files\Maven\apache-maven-3.9.9
C:\Program Files\Maven\
└── apache-maven-3.9.9\
├── bin\
│ ├── mvn.cmd ← The Maven executable
│ └── mvnDebug.cmd
├── boot\
├── conf\
│ └── settings.xml
└── lib\
Step 3: Configure the MAVEN_HOME variable
- Open the Start menu → type "environment variables"
- Click on "Edit the system environment variables"
- Click on "Environment Variables..."
- In System variables, click on New:
- Variable name:
MAVEN_HOME - Variable value:
C:\Program Files\Maven\apache-maven-3.9.9
- Variable name:
- Click on OK
Step 4: Add Maven to PATH
- In System variables, find the
Pathvariable - Click on Edit
- Click on New
- Add:
%MAVEN_HOME%\bin - Click on OK to close all windows
Step 5: Verify the installation
Close all open terminals, then open a new terminal (CMD or PowerShell):
mvn -version
Expected result:
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: C:\Program Files\Maven\apache-maven-3.9.9
Java version: 17.0.x, vendor: Eclipse Adoptium
Default locale: fr_CA, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
If mvn is not recognized after configuring the PATH, restart your terminal or even your computer. Environment variables are not reloaded in already open terminals.
Windows Alternative: Installation with Chocolatey
If you have Chocolatey installed, the installation is simpler:
choco install maven --version=3.9.9
Windows Alternative: Installation with Scoop
If you have Scoop installed:
scoop install maven
Installation on macOS
Option 1: Homebrew (Recommended)
Homebrew is the most popular package manager on macOS.
Install Homebrew (if not already done):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Maven:
brew install maven
Verify:
mvn -version
Homebrew automatically installs the latest stable version of Maven 3.9.x and configures the PATH for you.
Option 2: Manual Installation
Step 1: Download Maven
cd ~/Downloads
curl -O https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
Step 2: Extract the archive
sudo mkdir -p /opt/maven
sudo tar -xzf apache-maven-3.9.9-bin.tar.gz -C /opt/maven
Step 3: Configure environment variables
Open your shell profile file:
For zsh (macOS default since Catalina):
nano ~/.zshrc
For bash:
nano ~/.bash_profile
Add the following lines at the end of the file:
# Apache Maven
export MAVEN_HOME=/opt/maven/apache-maven-3.9.9
export PATH=$MAVEN_HOME/bin:$PATH
Save (Ctrl+O, Enter, Ctrl+X), then reload the profile:
source ~/.zshrc
Step 4: Verify
mvn -version
Expected result:
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /opt/maven/apache-maven-3.9.9
Java version: 17.0.x, vendor: Eclipse Adoptium
...
OS name: "mac os x", ...
Option 3: SDKMAN!
SDKMAN! is a tool that manages versions of Java, Maven, Gradle, etc.
# Install SDKMAN!
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
# Install Maven
sdk install maven 3.9.9
Installation on Linux
Option 1: Package Manager (Ubuntu/Debian)
sudo apt update
sudo apt install maven
Official Ubuntu/Debian repositories may contain an old version of Maven. Check the installed version:
mvn -version
If the version is lower than 3.9, prefer the manual installation below.
Option 2: Manual Installation (Recommended)
Step 1: Download Maven
cd /tmp
wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
Step 2: Extract and install
sudo mkdir -p /opt/maven
sudo tar -xzf apache-maven-3.9.9-bin.tar.gz -C /opt/maven
Step 3: Configure environment variables
Create a Maven configuration file:
sudo nano /etc/profile.d/maven.sh
Add the following content:
#!/bin/bash
export MAVEN_HOME=/opt/maven/apache-maven-3.9.9
export PATH=$MAVEN_HOME/bin:$PATH
Make the file executable and load it:
sudo chmod +x /etc/profile.d/maven.sh
source /etc/profile.d/maven.sh
Step 4: Verify
mvn -version
Expected result:
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /opt/maven/apache-maven-3.9.9
Java version: 17.0.x, vendor: Eclipse Adoptium
...
OS name: "linux", ...
Option 3: SDKMAN! (also works on Linux)
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install maven 3.9.9
Option 4: Fedora / CentOS / RHEL
sudo dnf install maven
Final Verification (all OS)
After installation, open a new terminal and run these commands:
# Verify Java
java -version
# Verify Maven
mvn -version
# Verify that JAVA_HOME is set
echo $JAVA_HOME # macOS/Linux
echo %JAVA_HOME% # Windows CMD
$env:JAVA_HOME # Windows PowerShell
Expected Result
You should see:
- Java: version 17 or higher
- Maven: version 3.9.x
- JAVA_HOME: points to your JDK installation
Maven requires the JAVA_HOME variable. If it is not set:
Windows: Add JAVA_HOME in the system environment variables, pointing to your JDK folder (e.g., C:\Program Files\Eclipse Adoptium\jdk-17.0.x)
macOS/Linux: Add in your ~/.zshrc or ~/.bashrc:
export JAVA_HOME=$(/usr/libexec/java_home -v 17) # macOS
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 # Linux (path may vary)
Test Maven: First Build
To confirm that Maven works, let's create a test project:
mvn archetype:generate \
-DgroupId=com.test \
-DartifactId=test-maven \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false
If Maven downloads dependencies and displays BUILD SUCCESS, your installation is complete.
cd test-maven
mvn clean package
[INFO] BUILD SUCCESS
[INFO] Total time: X.XXX s
You can delete this test folder:
cd ..
rm -rf test-maven # macOS/Linux
rmdir /s /q test-maven # Windows CMD
Summary of Commands by OS
| Action | Windows | macOS (Homebrew) | Linux (Manual) |
|---|---|---|---|
| Install | Download ZIP + configure PATH | brew install maven | wget + extract + PATH |
| Configure PATH | System environment variables | Automatic | /etc/profile.d/maven.sh |
| Verify | mvn -version | mvn -version | mvn -version |
| Update | Redownload | brew upgrade maven | Redownload |
Troubleshooting
mvn is not recognized
| Cause | Solution |
|---|---|
| PATH not configured | Add MAVEN_HOME/bin to PATH |
| Terminal not reloaded | Close and reopen the terminal |
| Wrong path | Check that the Maven bin folder exists at the configured path |
JAVA_HOME is not defined correctly
| Cause | Solution |
|---|---|
| JAVA_HOME not set | Set the JAVA_HOME environment variable |
| JAVA_HOME points to JRE | Point to the JDK (not the JRE) |
| Space in the path | Use quotes: "C:\Program Files\Java\jdk-17" |
Maven downloads slowly
The first Maven build downloads many dependencies into the ~/.m2/repository folder. This is normal and only happens once.
Validation Checklist
Before moving to Module 2, ensure everything is in place:
- Java 17+ installed and
java -versionworks - Maven 3.9.x installed and
mvn -versionworks -
JAVA_HOMEis set correctly - The
mvn archetype:generatetest worked - Visual Studio Code installed with Java extensions
Next Steps
Your environment is ready! You can now move to Module 2:
➡️ Module 2: Getting Started with Spring Boot