How to install maven in centos 7

The following sections provide a step by step instructions about how to install the latest Apache Maven version on CentOS 7. We’ll be downloading the latest release of Apache Maven from their official website.

1. Install OpenJDK

Maven 3.3+ requires JDK 1.7 or above to be installed. We’ll install OpenJDK, which is the default Java development and runtime in CentOS 7.

Install the OpenJDK package by typing:

sudo yum install java-1.8.0-openjdk

Verify that Java was successfully installed by running the following command:

java -version

The output should look something like this:

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)

Download Apache Maven

At the time of writing this article, the latest version of Apache Maven is 3.6.0. Before continuing with the next step, you should check the Maven download page to see if a newer version is available.

Start by downloading the Apache Maven in the /tmp directory using the following wget command:

wget https://www-us.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz -P /tmp

When the download is completed, extract the archive in the /opt directory:

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

To have more control over Maven versions and updates, we will create a symbolic link maven that will point to the Maven installation directory:

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

To upgrade your Maven installation, simply unpack the newer version and change the symlink to point to it.

3. Setup environment variables

Next, we’ll need to set up the environment variables. Open your text editor and create a new file named mavenenv.sh inside of the /etc/profile.d/ directory.

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

Paste the following lines:/etc/profile.d/maven.sh

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

Copy

Save and close the file. This script will be sourced at shell startup.

Make the script executable by running the following chmod command:

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

Load the environment variables using the source command:

source /etc/profile.d/maven.sh

4. Verify the installation

To verify that Maven is installed, use the mvn -version command which will print the Maven version:

mvn -version

You should see something like the following:

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T18:41:47Z)
Maven home: /opt/maven
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.3.2.el7.x86_64", arch: "amd64", family: "unix"

That’s it. The latest version of Maven is now installed on your CentOS system.