Install Maven in CentOS 7

Prerequisites:

  1. CentOS 7

  2. Root privileges

Steps:

  1. Install Java 8

  2. Download Maven

  3. Configure Maven Environment

  4. Check Maven Version

  5. Clean-up

Install Java 8

Install Java 8 on CentOS 7 using the yum command.

$ sudo yum install -y java-1.8.0-openjdk-devel

Check Version:

After the installation is complete, check the installed java version.

$ 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)

For more read this: Install Java 8 on CentOS 7

Download Maven:

Download Apache maven from its official website or use following command to download Apache Maven 3.6.0

$ cd /opt$ sudo wget http://www-eu.apache.org/dist/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz

Now extract downloaded archive using following command.

$ sudo tar xzf apache-maven-3.6.0-bin.tar.gz$ sudo ln -s apache-maven-3.6.0 maven

Configure Maven Environment:

We will define some environment variables that are needed by Apache Maven.

Step 1: Now set the environments variables by creating new file /etc/profile.d/maven.sh.

Step 2: Add the following configuration in ‘maven.sh’ configuration file.

export M2_HOME=/opt/mavenexport PATH=${M2_HOME}/bin:${PATH}

save and exit.

Step 3: Apply the configuration by running the ‘source’ command.

$ source /etc/profile.d/maven.sh

Check Maven Version:

Apache Maven has been successfully configured on your system. Use the following command to check the version of installed Maven on your system.

$ mvn --version
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-1.el7_6.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"

Clean-up

Finally, remove the downloaded archive file from the system to free disk space.

$ sudo rm -f /opt/apache-maven-3.6.0-bin.tar.gz