How to Install Apache Maven on CentOS 8/RHEL 8

In this article we are going to perform Downloading latest maven on CentOS, How to Install Apache Maven on CentOS 8/RHEL 8, Setup Apache Maven Environment Variables and check maven version on centos 8.

What is Maven ?

Maven is a open source build tool which helps in build and project management mainly in Java projects, Maven is licensed under Apache and written in Java and C# programming languages.

Apache Maven uses a Project Object Model (POM) which contains all this required information which helps in management and documentation of project.

While developing a project we may need many JAR files and different libraries which will need to import in project, all that Jar files and libraries we can easily download and install in project from Apache Maven.

What is Project Object Model (pom.xml) in Maven ?

Project Object Model (POM) consists of a configuration file of a project with various essential information, when we execute Maven command, the execution will based on the classifications which are described in POM file.

Maven Lifecycle ?

Below are phases of Maven Lifecycle.

How to Install Apache Maven on CentOS 8/RHEL 8 1
  1. Validate – This is the first phase of Maven Lifecycle, In this phase validation of project is done weather all needed data is available or not.

2. Compile – In this phase, Error checking, bug checking, compilation of the project source code is done.

3. Test – As in second phase we compiled the source code and now we will test the compiled source code using Unit testing.

4. Package – Now unit testing is done on compiled source code so it has to be merged into package in its format. For eg- JAR file packages.

5. Verify – In this phase of Maven Lifecycle, checks are executed and verified on the results of integration testing which ensures quality is met.

6. Install – In this Install phase, we will install the packages in our local system’s repository which can be used as dependencies for other local projects.

7. Deploy – It is the last phase in which we copies the final package after done the built to the remote repository for sharing with team.

How to Install Apache Maven on CentOS 8 using dnf/yum

To install Apache maven on CentOS 8 using dnf

sudo dnf install maven

To install Apache maven on CentOS 8 using yum

sudo yum install maven

Output:

Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_292, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-0.el8_3.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-193.6.3.el8_2.x86_64", arch: "amd64", family: "unix"

#1. How to Install Apache Maven on CentOS 8/RHEL 8 by downloading Apache Maven Binaries

update the system packages

sudo yum update

Install OpenJDK 11 on CentOS 8 /RHEL 8

sudo yum install java-11-openjdk

Check the Install OpenJDK version using command line

java --version

Output:

openjdk 11.0.11 2021-04-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.11+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.11+9-LTS, mixed mode, sharing)

#2. Download Latest Apache Maven on CentOS 8 /RHEL 8

Download the latest Apache Maven version on Ubuntu from Apache Maven official download page.

Navigate to /tmp directory

cd /tmp

Download the Apache Maven version using wget

sudo wget https://www-eu.apache.org/dist/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz

once downloaded extract the apache maven setup in /opt directory

sudo tar xf /tmp/apache-maven-*.tar.gz -C /opt

Create a symlink for apache maven directory to update the maven versions

sudo ln -s /opt/apache-maven-3.8.1 /opt/maven

#3. Setup Apache Maven Environment Variables

To setup apache maven environment variables in CentOS 8/RHEL 8, create a file named maven.sh in /etc/profile.d/ directory

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

Paste the below lines

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

Give the executable permission using chmod

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

Load the Environment Variables using source command

source /etc/profile.d/maven.sh

Verify the installed maven version using command line

mvn -version

Output:

Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /opt/maven
Java version: 11.0.11, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.11.0.9-0.el8_3.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-193.6.3.el8_2.x86_64", arch: "amd64", family: "unix"

So now we will remove the Apache Maven downloaded archive file because it is of no use now.

lets free up space by using below command which will remove the downloaded archive file.

sudo rm -f apache-maven-3.8.1-bin.tar.gz

Conclusion:

We have covered How to Install Maven on CentOS 8/RHEL 8 , configuring environment variables for maven and check maven version using command line.

Related Articles

Akash Padwal

I am Akash Padwal working as a DevOps Engineer Intern in Product Company. Likes to explore new DevOps tools and share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap