How to Setup Apache Maven on CentOS 7

Photo of author

By rasho

Apache Maven is a software project management tool based on the POM (Project, Object, Model) concept. Maven is a build automation tool used primarily for Java-based projects, but can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. It not only describes how software builds but also its dependencies.
In this article, I will explain how to install and configure latest version of Apache Maven on a CentOS 7 system.

Install OpenJDK 8 in CentOS 7

The first thing we will do here is to install Java on the CentOS 7 server. Apache Maven requires JDK 1.7 or above – we will be using Java 8 that can be installed from the CentOS repository. Install Java 8 on CentOS 7 using the yum command.

# yum install -y java-1.8.0-openjdk-devel

After the installation is complete, check the installed java version

# java -version

And you will get the result as below.

Check the installed java version
Check the installed java version

Java OpenJDK 8 has been installed on CentOS 7.

Install Apache Maven in CentOS 7

Next, go to the official Apache Maven download page and grab the latest version or use the following wget command to download it under the maven home directory “/usr/local/src“.

# cd /usr/local/src
# wget http://apache.mirror.ba/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz

Extract the downloaded archive file, and rename it using following commands.

# tar -xf apache-maven-3.6.0-bin.tar.gz
# mv apache-maven-3.6.0/ apache-maven/

[ads]

Configure Apache Maven Environment

In this step, we will configure the environment for Apache Maven. We will define some environment variables that are needed by Apache Maven.
Go to the “/etc/profile.d” directory and create a new configuration file “maven.sh”.

# cd /etc/profile.d/
# vi maven.sh

Paste the following configuration there.

# 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}

Save and exit.
Now make the ‘maven.sh’ script executable and then apply the configuration by running the ‘source’ command.

# chmod +x maven.sh
# source /etc/profile.d/maven.sh

Testing

To verify our Apache Maven installation, you can run the maven command below.

# mvn --version

And you should get a result similar to the following:

[root@lintut ~]# mvn --version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T18:41:47Z)
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-957.1.3.el7.x86_64", arch: "amd64", family: "unix"

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

# rm -f /usr/local/src/apache-maven-3.6.0-bin.tar.gz

Apache Maven 3.6.0 installation has been completed. It’s running under Linux CentOS 7 64bit, with Java 1.8 installed, and the Maven Home directory is “/usr/local/src/apache-maven“.
If you have any problems related to installation, do share with us in the comment section.

Leave a Comment