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. Basically, it is a software project management tool based on the POM (Project, Object, Model) concept.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Apache Maven queries on Linux systems.
In this context, we shall look into how you can install Apache Maven on Rocky Linux 8.4.
You can install Apache Maven via different methods such as through the DNF / YUM package manager or install from the source binary package which gives you the latest version.
To install Apache Maven using DNF, run the command:
$ sudo dnf install maven
This installs Apache Maven and associated dependencies.
Note that this method will not always give you the latest release of Maven.
To confirm the version of Apache version installed, execute the command:
$ mvn --version
Using this method, you can Install the latest version of Apache Maven. To do this, follow the steps given below.
i. Update the system
First, update the packages on your system:
$ sudo dnf update
ii. 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
iii. 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
iv. 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
v. Verify Apache Maven Installation
Here, you can verify Apache Maven Installation by running this command:
$ mvn --version
This article covers how to install Apache Maven on Rocky Linux. You can now visit the Apache Maven Documentation page to learn how you can get started with managing Java projects with Apache Maven. In fact, Apache Maven Maven is a build automation tool used primarily for Java-based projects, but can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.