Apache Maven is a free and powerful build automation tool primarily used for Java projects. It is based on the Project Object Model. With maven, you can efficiently manage a project’s build, reporting, and documentation.
Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages.
This post will help you install Apache Maven on CentOS 8 / RHEL 8.
Install Java
Contents
Java development kit is the essential requirement for Apache Maven to build projects. Either install Oracle JDK or OpenJDK on your system.
READ: How To Install Oracle Java CentOS 8 / RHEL 8
OR
If you plan to use OpenJDK for Maven, then you can use the DNF command to install it.
dnf -y install java-1.8.0-openjdk-devel
Verify the JDK installation with the below command.
java -version
Output:
openjdk version "1.8.0_232" OpenJDK Runtime Environment (build 1.8.0_232-b09) OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
Install Apache Maven
You can go to Apache Maven’s official website to download the latest stable version of Maven or use the following command to download the Apache Maven v3.6.3.
dnf -y install wget wget https://www-eu.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
Now extract the downloaded archive using the tar command and then move them to the desired location.
tar -zxvf apache-maven-3.6.3-bin.tar.gz mv apache-maven-3.6.3 /opt/maven
Setup Environment Variables
We will now set the environment variables for Maven by creating a maven.sh file under /etc/profile.d/ directory.
vi /etc/profile.d/maven.sh
Add the following content.
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-2.el8_1.x86_64/ export M2_HOME=/opt/maven export PATH=${M2_HOME}/bin:${PATH}
JAVA_HOME and M2_HOME will depend on JDK and Maven installation.
Now load the environment variables in the current shell using the following command.
source /etc/profile.d/maven.sh
Verify Maven Installation
Check whether the Apache Maven has been successfully configured on your system using the following command.
mvn -version
Output:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /opt/maven Java version: 1.8.0_232, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-2.el8_1.x86_64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.18.0-80.11.2.el8_0.x86_64", arch: "amd64", family: "unix"
Conclusion
That’s All. I hope you learned how to install Apache Maven on CentOS 8 / RHEL 8.