There is a new version of this tutorial available for CentOS 8.

How to Install Apache Maven on CentOS 7

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 tutorial, we will show you step-by-step how to install and configure Apache Maven on a CentOS 7 system.

Prerequisites

  • CentOS 7 Server
  • Root privileges

What we will do

  1. Install Java OpenJDK on CentOS 7
  2. Download Apache Maven Binary Files
  3. Configure Apache Maven Environment
  4. Testing

Step 1 - Install Java OpenJDK on 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.

sudo 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.

Install Java OpenJDK on CentOS 7

Java OpenJDK 8 has been installed on CentOS 7.

Step 2 - Download Apache Maven

In this step, we will download the apache maven binary code using the wget command. And we will be using the '/usr/local/src' directory as a maven home directory.

Go to the '/usr/local/src' directory and download the Apache Maven Binary code.

cd /usr/local/src
wget http://www-us.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

Extract the maven.tar.gz file, and then delete the compressed file.

tar -xf apache-maven-3.5.2-bin.tar.gz
rm -f apache-maven-3.5.2-bin.tar.gz

And you will get a new directory apache-maven-version - rename the directory.

mv apache-maven-3.5.2/ apache-maven/

Apache Maven is now downloaded in the '/usr/local/src/apache-maven' directory.

Download Apache Maven

Step 3 - 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/
vim 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

The Apache Maven environment configuration has been completed.

Configure Apache Maven Environment

Step 4 - 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: 

Test apache maven

Apache Maven 3.2 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'.

Reference

Share this page:

5 Comment(s)