How to Install Apache Maven on AlmaLinux 8

Apache Maven is a powerful project management and comprehension tool used by software developers working with the Java programming language. It is a build automation tool that favors convention over configuration, and is based on the concept of a project object model (POM).

Maven adopts the concept of software construction using a Project Object Model(POM) to describe the project structure. A POM is roughly equivalent to (and replaces) the older concept of Makefile. Each POM describes an individual project, and details on how it may be built.

Maven follows some fundamental concepts which make it superior to its predecessor, Make. The first is the use of a declarative XML specification for describing the project structure, dependency management, and build process. This allows Maven to intelligently manage dependencies on external libraries/frameworks as well as reproduce the project environment on different machines.

Second, all the project information is stored in a concise file which captures all the information required to manage it. This also helps users easily identify possible problems, by browsing through the POM files and looking at their dependencies.

Maven is an ideal tool for managing projects where there are separate development, test, and production environments. This article will show you how to install the Apache Maven package on AlmaLinux 8 so that it may be used for your projects.

Prerequisites

  • A server running AlmaLinux 8.
  • Root access on the server.

Updating the System

If you’ve not updated your system for a while, there may be security vulnerabilities which you need to address and close. It is strongly advised that you should update your package periodically. Run the command below to update your system.

sudo dnf update -y
sudo dnf upgrade -y

Installing Java

Java Development Kit (JDK) and Java Runtime Environment (JRE) will be installed to provide the bytecode needed for running java programs on your system. Maven is written in Java, so Java must be installed before you can continue with the installation of Maven.

To install the open-source version of JRE/JDK, run the command below.

sudo dnf install java-11-openjdk-devel -y

Once the installation is complete, verify that Java has been installed by running the command below. If JRE/JDK is successfully installed, you will see its version displayed on your screen.

java -version

Sample output:

Installing Java

Installing Maven Via DNF

This is the easiest way to install Maven on your AlmaLinux 8 server because the official AlmaLinux repositories already contain a Maven package. So, you don't have to add any external repositories to gain access to Maven.

However, the current package available in the official AlmaLinux repositories may not be the latest version of Maven. To install the latest stable Maven release, skip this method and go straight to the next section.

Run the command below to install Maven on your AlmaLinux 8 system.

sudo dnf install maven -y

Once the installation is completed, you can test your installation by running the mvn command with the -version option.

mvn -version 

You should see the current version of Maven being displayed on the screen. Your version may be different from the one shown in the following screenshot. You can see Apache Maven 3.5.4 (Red Hat 3.5.4-5) being displayed in the screenshot below.

Apache Maven version

Installing Apache Maven from Source

Installing Maven from the source is a bit more complex than installing it via yum or dnf, but you will get access to the latest stable version of Maven.

It requires the user to download an archive containing the source code for Maven, extract that archive to a directory on your server, build Maven from the source and then install it. For the benefit of those who are interested in installing Maven from its source, this section will show you how to do just that.

First, we need to download the latest stable version of Apache Maven. You can download this release from the official Maven website using the command below.

wget https://dlcdn.apache.org/maven/maven-3/3.8.3/binaries/apache-maven-3.8.3-bin.tar.gz

Once the download has been completed, extract the archive to the /opt/ directory on your server using the command below.

sudo tar -xvf apache-maven-3.8.3-bin.tar.gz -C /opt
sudo ln -s /opt/apache-maven-3.8.3 /opt/maven

This will extract the contents of the archive to a directory called apache-maven in the /opt/ directory on your server. We need to add this path to our PATH environment variable so that we can access the mvn command from any directory on our server. Run the command below to create the PATH variable.

sudo nano /etc/profile.d/maven.sh

Add the following line at the end of this file and save it by pressing CTRL+X, press Y and then press Enter to confirm.

export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Now, run the following command to make your script executable and load the environment variables for the current session.

sudo chmod +x /etc/profile.d/maven.sh && source /etc/profile.d/maven.sh

Now that the PATH environment variable has been updated let's test if Maven is working correctly by running the command below.

mvn -version

You should see the output in the screenshot below. You can see that Apache Maven 3.8.3 is displayed on your screen if everything goes right. This version is more new than the one we installed earlier via dnf.

Installing Apache Maven from Source

Conclusion

In this article, we have learnt how to install Maven on an AlmaLinux 8 server using dnf or from the source. If you found this article useful, please share it with other users via social media. If you have any questions or feedback, feel free to let us know in the comment section below.

Share this page:

0 Comment(s)