Easy Steps To Install Apache Maven on AlmaLinux 9
In this comprehensive guide, we will explore How To Install Apache Maven on AlmaLinux 9. Apache Maven is a powerful, open-source build automation tool primarily used for Java projects. It streamlines the build process, manages dependencies, and simplifies project management, making it an indispensable asset for developers. Maven’s foundation lies in the Project Object Model (POM), which allows for consistent and repeatable builds. This article will guide you through the installation and basic usage of Maven on AlmaLinux 9.
Maven, written in Java, extends its capabilities beyond Java projects to build projects written in C#, Scala, Ruby, and more. It centralizes project configuration, dependency management, and build processes. Maven has greatly simplified tasks such as generating reports, conducting checks, and automating build and testing procedures for Java developers.
Let’s delve into the step-by-step instructions to Install Apache Maven on AlmaLinux 9, ensuring a smooth setup process.
Before we begin, ensure you have access to an AlmaLinux 9 server and are logged in as a non-root user with sudo
privileges. You can refer to our guide on Initial Server Setup with AlmaLinux 9 for assistance with this initial configuration.
1. Install OpenJDK For Apache Maven Setup
Since Maven is a Java-based tool, you need to install Java Development Kit (JDK) on your AlmaLinux 9 system. First, update the package index:
sudo dnf update -y
Maven 3.x requires JDK 1.7 or later. Install OpenJDK 8 using the following command:
sudo dnf install java-1.8.0-openjdk -y
Verify the Java installation by checking the version:
java -version
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-b08)
OpenJDK 64-Bit Server VM (build 25.362-b08, mixed mode)

2. Apache Maven Setup on AlmaLinux 9
This section guides you through downloading, installing, and configuring Maven on AlmaLinux 9.
Apache Maven Download
Visit the Apache Maven downloads page and copy the link address of the latest binaries package. Download the package to the /tmp
directory using the wget
command:
sudo wget https://dlcdn.apache.org/maven/maven-3/3.8.7/binaries/apache-maven-3.8.7-bin.tar.gz -P /tmp
Extract the downloaded archive to the /opt
directory:
sudo tar xf /tmp/apache-maven-3.8.7-bin.tar.gz -C /opt
Create a symbolic link to the Maven installation directory:
sudo ln -s /opt/apache-maven-3.8.7 /opt/maven
Note: When upgrading Maven, unpack the newer version and update the symlink to point to it.
Set up Maven Environment Variables
Configure Maven’s environment variables. Create a new file named maven.sh
in the /etc/profile.d
directory:
sudo vi /etc/profile.d/maven.sh
Add the following lines to the file:
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Save the file and exit the editor.
Set Correct Permissions For Maven File
Make the maven.sh
file executable:
sudo chmod +x /etc/profile.d/maven.sh
Load Maven Variables
Load the Maven environment variables:
source /etc/profile.d/maven.sh
Verify the Maven installation by checking the version:
mvn -version
Apache Maven 3.8.7 (b61a8741ea61d5f3b8a1fbf83f7b83cc5ba049a2)
Maven home: /opt/maven
Java version: 1.8.0_362, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-1.el9_2.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.14.0-284.25.1.el9_2.x86_64", arch: "amd64", family: "unix"

With these steps completed, you have successfully performed How To Install Apache Maven on AlmaLinux 9.
3. Test Maven Installation on AlmaLinux 9
To confirm that Maven is functioning correctly, create a simple "Hello World" project. Execute the following command:
mvn archetype:generate -DgroupId=com.jcg.maven -DartifactId=MavenHelloWorldProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No version)
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> archetype:generate {execution: default-cli} @ standalone-pom >>>
[INFO]
[INFO] <<< archetype:generate {execution: default-cli} @ standalone-pom <<<
[INFO]
[INFO] --- archetype:generate {execution: default-cli} @ standalone-pom ---
[INFO] Generating project in C:UsersexampleMavenHelloWorldProject
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.jcg.maven
[INFO] Parameter: artifactId, Value: MavenHelloWorldProject
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.jcg.maven
[INFO] Parameter: interactiveMode, Value: false
[INFO] Parameter: archetypeArtifactId, Value: maven-archetype-quickstart
[INFO] Parameter: archetypeGroupId, Value: org.apache.maven.archetypes
[INFO] Parameter: archetypeVersion, Value: 1.4
[INFO] Project created from Archetype in dir: C:UsersexampleMavenHelloWorldProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.095 s
[INFO] Finished at: 2024-01-24T14:47:01+08:00
[INFO] ------------------------------------------------------------------------

Navigate to the newly created project directory:
cd MavenHelloWorldProject
List the contents of the directory:
ls -l
total 4
drwxrwxr-x. 2 user user 6 Nov 20 14:53 src
-rw-rw-r--. 1 user user 222 Nov 20 14:53 pom.xml

The src
directory contains the source code, while pom.xml
is Maven’s project configuration file.
Inspect the pom.xml
file:
sudo cat pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jcg.maven</groupId>
<artifactId>MavenHelloWorldProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenHelloWorldProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Compile the Project with Maven
Compile the project to generate a JAR file:
mvn clean install
After successful compilation, check the target
directory for the JAR file:
ls -l target
Alternative Solutions to Install Apache Maven on AlmaLinux 9
While the provided method involves downloading and manually configuring Maven, there are alternative, potentially simpler approaches:
1. Using SDKMAN! (Software Development Kit Manager):
SDKMAN! is a versatile tool for managing multiple software development kits, including Maven. It simplifies installation, version management, and switching between different SDK versions.
-
Explanation: SDKMAN! automates the download, installation, and environment variable configuration for Maven. It also allows you to easily switch between different Maven versions, which can be useful for projects with specific requirements.
-
Installation:
curl -s "https://get.sdkman.io" | bash source "$HOME/.sdkman/bin/sdkman-init.sh" sdk install maven
This script downloads and installs SDKMAN!. Follow the on-screen instructions to complete the installation. Once SDKMAN! is installed, use it to install Maven. SDKMAN! will handle setting up the necessary environment variables.
To verify that Maven is installed correctly, run:
mvn -version
2. Using a Package Manager (If Available):
While AlmaLinux 9 doesn’t directly offer Maven through its default repositories, it’s worth exploring if any third-party repositories provide it, or if a custom RPM package is available. This approach would leverage the system’s package manager for installation and updates.
-
Explanation: Package managers like
dnf
handle dependencies and updates automatically. If a suitable RPM package for Maven is available, installing it throughdnf
would be straightforward and ensure proper integration with the system. -
Check for Availability (Example):
First, search for Maven in available repositories:
dnf search maven
If a package is found, install it using:
sudo dnf install <package_name>
Replace
<package_name>
with the actual name of the Maven package. However, note that this approach might install an older version of Maven depending on the repository.
Conclusion
This guide has demonstrated How To Install Apache Maven on AlmaLinux 9 and test the installation by creating a simple project. While manual installation provides control, alternative methods like SDKMAN! can streamline the process. Remember to choose the method that best suits your needs and environment.
You may also like these articles:
How To Install AnyDesk on AlmaLinux 9