Support Online
Skip to main content

Apache Maven Installation for Java Developers (Ubuntu, JDK 17, SDKMAN and CI/CD)

🎯 What Will You Learn in This Guide?

This guide is a step-by-step guide to installing Apache Maven 3.9.9 on Ubuntu and configuring JDK 17 (LTS).
It also explains comparatively SDKMAN, manual installation and CI/CD automation methods.

With this guide:

  • ☕ You will complete the Maven and JDK 17 installation safely.
  • ⚙️ You will learn how to set JAVA_HOME and M2_HOME variables correctly.
  • 🧠 You will understand how to integrate Maven into CI/CD pipelines.

🧱 1. Prerequisites

What you need:

  • Ubuntu 22.04+ (example: GenixNode virtual server)
  • sudo privilege
  • Basic terminal information
  • Internet connection

☕ 2. JDK 17 Installation

Maven requires the Java Development Kit (JDK) to run.
JDK 17 is the recommended version as it offers long-term support (LTS).

sudo apt update
sudo apt install openjdk-17-jdk -y

2.2 Setting JAVA_HOME Variable


echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' >> ~/.profile
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.profile
source ~/.profile
java -version

✅ If you see "openjdk version 17.0.x" in the output, the installation is complete.


🧰 3. Apache Maven Installation Methods

🔹 Method A: APT (Easy and Safe)


sudo apt update
sudo apt install maven -y
mvn -version

⚠️ Note: The version in Ubuntu repositories is usually a few versions behind.

🔹 Method B: Installing from Binary (Latest Version)


wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
tar -xvf apache-maven-3.9.9-bin.tar.gz
sudo mv apache-maven-3.9.9 /opt/

Set environment variables:


echo 'export M2_HOME=/opt/apache-maven-3.9.9' >> ~/.profile
echo 'export PATH="$M2_HOME/bin:$PATH"' >> ~/.profile
source ~/.profile
mvn -version

🔹 Method C: SDKMAN (For Release Management)


curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install maven 3.9.9
mvn -version

The version can be changed instantly with the sdk use maven <sürüm> command.


🧪 4. Test Maven Installation (Hello World)

Create Project:


mkdir maven-ilk-proje && cd maven-ilk-proje
mvn archetype:generate -DgroupId=com.genixnode -DartifactId=merhaba-dunya \
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Compile and Package:


cd merhaba-dunya
mvn package
java -cp target/merhaba-dunya-1.0-SNAPSHOT.jar com.genixnode.App

✅ Expected output:

Hello World!

🤖 5. CI/CD Integration and Performance Optimization

Dockerfile Example:


FROM ubuntu:22.04
RUN apt-get update && apt-get install -y openjdk-17-jdk maven
WORKDIR /app
COPY . .
RUN mvn -B clean install

💡 This framework ensures isolated and repeatable builds in CI/CD pipelines.

Speed Increase with Parallel Compilation


mvn -T 1C clean install

The -T 1C parameter speeds up compilation by turning on one thread per CPU core.


❓ Frequently Asked Questions (FAQ)

1. Why are JAVA_HOME and M2_HOME important?

JAVA_HOME ensures Maven finds the correct JDK. M2_HOME indicates the Maven installation directory.

2. What should I do if the mvn command is not found?

Reload environment variables with the source ~/.profile command. Make sure the outputs of echo $JAVA_HOME and echo $M2_HOME are correct.

3. Which method is recommended for a production environment?

SDKMAN or manual binary installation provides the latest versions and full control.

APT is only suitable for simple, single-server installations.

4. What is the “Could not find artifact” error?

Occurs when Maven cannot find the dependency.

Check your internet connection.

Clean the ~/.m2/repository directory.

Verify proxy settings in ~/.m2/settings.xml.


🏁 Result

You have now completed the installation of Apache Maven 3.9.9 + JDK 17 on your Ubuntu system. With this infrastructure, you can compile Java projects, create CI/CD pipelines and test them safely on GenixNode. 🚀

☁️ You can automatically configure Maven and JDK environments on the GenixNode infrastructure and run them in seconds.