CentOS

How to Install Apache Maven on Rocky Linux / CentOS 8

How to Install Apache Maven on Rocky Linux / CentOS 8

Written in Java and licensed under Apache License v2, Apache Maven is an open-source software project management and build tool that is tailored specifically for Java projects. In this guide, we look at how you can install Apache Maven on Rocky Linux 8.4.

There are two ways of installing Apache Maven: you can install using the DNF / YUM package manager or install from the source binary package which gives you the latest version. We will look at each of the installation methods in turn.

Method 1: Install Apache Maven using DNF / YUM package manager

This is quite a straightforward way of installing Maven. However, be advised that this will not always give you the latest release of Maven. To install Apache Maven using DNF, run the command:

$ sudo dnf install maven

This installs Apache Maven and associated dependencies.

To confirm the version of Apache version installed, execute the command:

$ mvn --version

Method 2: Install the latest version of Apache Maven

If getting the latest release of Apache Maven is your top priority, then installing from source is the way to go. This is accomplished by a series of steps as we shall see shortly.

Step 1: Update the system

First, update the packages on your system:

$ sudo dnf update

Step 2: Install OpenJDK

Maven 3.3 and later versions require OpenJDK 1.7 and recent versions to install. We will install the latest LTS release which, at the moment, is OpenJDK 11. To achieve this, run the command:

$ sudo dnf install java-11-openjdk-devel

To confirm the version of OpenJDK installed, run:

$ java -version

Step 3: Download Apache Maven archive

The next step is to download Apache Maven. The latest version, at the time of writing this guide, is Apache Maven 3.8.2. By the time you are reading this guide, chances are that another version will be the latest release. So, check Apache Maven’s download page.

To download the Archive file, run:

$ wget https://downloads.apache.org/maven/maven-3/3.8.2/binaries/apache-maven-3.8.2-bin.tar.gz

Next, extract the archive to the /opt directory.

$ sudo tar -xvf apache-maven-3.8.2-bin.tar.gz -C /opt

Thereafter, create a symbolic link that points to the installation directory

$ sudo ln -s /opt/apache-maven-3.8.2 /opt/maven

Step 4: Configure environment variables

The next course of action is to set up or configure environment variables. First, create the maven.sh shell script file.

$ sudo vim /etc/profile.d/maven.sh

Paste these lines and save 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}

Next, make the script executable.

$ sudo chmod +x /etc/profile.d/maven.sh

Then load the environment variables using the source command.

$ source /etc/profile.d/maven.sh

Step 5: Verify installation

At this point, the installation of Apache Maven is complete. As before, you can verify this using the command:

$ mvn --version

This wraps up this guide on how to install Apache Maven on Rocky Linux. You can now visit the Documentation page to learn how you can get started with managing Java projects with Apache Maven.

Similar Posts