How to install Apache Maven on CentOS 7

Apache Maven Installation on CentOS 7

Apache Maven is a open source software project management and build automation tool, that is based on the conception of a project object model (POM), which is primarily used for deploying Java-based applications. This tutorial covers the method to install Apache Maven on CentOS 7.

Prerequisites

Java Development Kit (JDK)

Installation

Let's First install Java on CentOS 7 system from the default repository and verify the version using following commands

[root@linuxhelp1 ~]# yum install -y java-1.8.0-openjdk-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.dhakacom.com
 * epel: mirror.premi.st
 * extras: mirror.dhakacom.com
 * updates: mirror.dhakacom.com
Resolving Dependencies
--> Running transaction check
.
.
.
  java-1.8.0-openjdk-headless.x86_64 1:1.8.0.191.b12-1.el7_6                                  
  nspr.x86_64 0:4.19.0-1.el7_5                                                                
  nss.x86_64 0:3.36.0-7.el7_5                                                                 
  nss-softokn.x86_64 0:3.36.0-5.el7_5                                                         
  nss-softokn-freebl.x86_64 0:3.36.0-5.el7_5                                                  
  nss-sysinit.x86_64 0:3.36.0-7.el7_5                                                         
  nss-tools.x86_64 0:3.36.0-7.el7_5                                                           
  nss-util.x86_64 0:3.36.0-1.el7_5                                                            

Complete!

Verify the version of Java which you have just installed

[root@linuxhelp1 ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

Next, we need to download the apache maven package under the maven home directory ‘/usr/local/src’

[root@linuxhelp1 ~]# cd /usr/local/src
[root@linuxhelp1 src]# wget http://www-us.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
--2019-01-19 16:04:54--  http://www-us.apache.org/dist/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
Resolving www-us.apache.org (www-us.apache.org)... 40.79.78.1
Connecting to www-us.apache.org (www-us.apache.org)|40.79.78.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8842660 (8.4M) [application/x-gzip]
Saving to: ‘apache-maven-3.5.4-bin.tar.gz’

100%[====================================================>] 8,842,660   51.3MB/s   in 0.2s   

2019-01-19 19:45:21 (51.3 MB/s) - ‘apache-maven-3.5.4-bin.tar.gz’ saved [8842660/8842660]

Extract the downloaded package using tar command

[root@linuxhelp1 src]# tar -xvf apache-maven-3.5.4-bin.tar.gz
apache-maven-3.5.4/README.txt
apache-maven-3.5.4/LICENSE
apache-maven-3.5.4/NOTICE
apache-maven-3.5.4/lib/
apache-maven-3.5.4/lib/cdi-api.license
apache-maven-3.5.4/lib/commons-cli.license
apache-maven-3.5.4/lib/commons-io.license
apache-maven-3.5.4/lib/commons-lang3.license
apache-maven-3.5.4/lib/jcl-over-slf4j.license
.
.
.
apache-maven-3.5.4/lib/jcl-over-slf4j-1.7.25.jar
apache-maven-3.5.4/lib/wagon-file-3.1.0.jar
apache-maven-3.5.4/lib/maven-resolver-connector-basic-1.1.1.jar
apache-maven-3.5.4/lib/maven-resolver-transport-wagon-1.1.1.jar
apache-maven-3.5.4/lib/maven-slf4j-provider-3.5.4.jar
apache-maven-3.5.4/lib/jansi-1.17.1.jar

Now we need to configure the environments variables, create a configuration file ‘maven.sh’ in the ‘/etc/profile.d’ directory & Switch in to the directory as follows

[root@linuxhelp1 src]# cd /etc/profile.d/

And configure the file as Follows

[root@linuxhelp1 profile.d]# vim maven.sh
#Apache Maven Environment Variables
#MAVEN_HOME for Maven 1 - M2_HOME for Maven 2
export M2_HOME=/usr/local/src/apache-maven
export PATH=${M2_HOME}/bin:${PATH}

Now make the ‘maven.sh’ configuration file in executable Format

[root@linuxhelp1 profile.d]# chmod +x maven.sh

Then load the configuration by running the ‘source’command

[root@linuxhelp1 profile.d]# source /etc/profile.d/maven.sh

To verify Apache Maven installation, run the following maven command

[root@linuxhelp1 profile.d]# mvn --version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T00:18:14+05:45)
Maven home: /usr/local/src/apache-maven
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"

With this the method to install Apache Maven on centos 7 comes to an end.

FAQ
Q
Are there any alternatives to apache maven?
A
The alternatives to Apache Maven are
CMake
Gradle
SCons
Ant
Q
What is JAR file in Java?
A
A JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.
Q
Why do we use Maven in Java?
A
It is used for projects build, dependency and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT. In short terms we can tell maven is a tool that can be used for building and managing any Java-based project.
Q
What are the uses of Apache Maven?
A
Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: first, it describes how software is built, and second, it describes its dependencies.
Q
What is Apache Maven?
A
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM).