Home » Maven: Install Apache Maven on Linux – CentOS 7

Maven: Install Apache Maven on Linux – CentOS 7

by Pring
Maven: Install Apache Maven on Linux – CentOS 7

I. Introduction.

This tutorial will teaches you how to install Apache Maven on Linux – CentOS 7 server. Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

II. Prerequisites.

1. Login to your system via SSH. You can use PuTTy tool or Poderosa tool,…
2. Login with root user.  If you are logged in as another user, you will need to add sudo before each command.
3. Your system has installed the Oracle Open Java JDK. In the case not installed yet, you must install it.

III. Install Maven on CentOS 7 step by step.

To install Apache Maven on Linux – CentOS 7, we can use the following two ways:

Option 1: Using yum is a simple, follow these steps:
Step 1. Install Apache Maven.
Step 2. Verify the installation.


Option 2: Using Apache Maven package, follow these steps:
Step 1. Download Apache Maven.
Step 2. Extract downloaded file.
Step 3. Setup Environment variables.
Step 4. Verify the installation.

You can see details of Video on How to install Maven on Linux – CentOS 7 as below.
[Sorry, the video we will update later]

Or you can see the details and follow the steps below.

Option 1: Using yum to install Apache Maven.

Step 1. Install Apache Maven.

Enter command below:

sudo yum install maven

Step 2. Verify the installation.

Enter command below:

mvn -version

Sample Output:

[root@devopsio ~]# mvn -version
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 1.8.0_222, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

To uninstall Apache Maven. Enter the command below:

sudo yum remove maven

Option 2: Using Apache Maven package.

Step 1. Download Apache Maven.

1. Get Dowload link. Access Apache Maven page: https://maven.apache.org/download.cgi
Maven: Install Apache Maven on Linux – CentOS 7

2. The copied download link is [ https://www-us.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz ].
Start by downloading the Apache Maven in the [ /tmp ] directory using the following wget command:
Enter command below:

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

If you see an error as follows:

[root@devopsio tmp]# wget https://www-us.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
-bash: wget: command not found

You must install the wget command. Enter the command below:

sudo yum install wget

Sample Output:

[root@devopsio tmp]# yum install wget
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================
 Package           Arch                Version                      Repository            Size
===============================================================================================
Installing:
 wget              x86_64              1.14-18.el7_6.1              updates              547 k

Transaction Summary
===============================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Is this ok [y/d/N]: y
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm                                         | 547 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-18.el7_6.1.x86_64                                                 1/1 
  Verifying  : wget-1.14-18.el7_6.1.x86_64                                                 1/1 

Installed:
  wget.x86_64 0:1.14-18.el7_6.1                                                                

Complete!

OK, after install wget command finished. Rerun the following command [ wget https://www-us.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz ]

Sample Output:

[root@devopsio tmp]# wget https://www-us.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz
--2019-08-05 15:02:12--  https://www-us.apache.org/dist/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-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|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9136463 (8.7M) [application/x-gzip]
Saving to: ‘apache-maven-3.6.1-bin.tar.gz’

100%[=====================================================>] 9,136,463   18.5MB/s   in 0.5s   

2019-08-05 15:02:12 (18.5 MB/s) - ‘apache-maven-3.6.1-bin.tar.gz’ saved [9136463/9136463]
[root@devopsio tmp]# pwd
/tmp
[root@devopsio tmp]# ls -lrt
total 8924
-rw-r--r--. 1 root root 9136463 Apr  4 19:30 apache-maven-3.6.1-bin.tar.gz

Step 2. Extract downloaded file.

When the download is completed, extract [ apache-maven-3.6.0-bin.tar.gz ] file into the [ /opt ] directory:

sudo tar xf /tmp/apache-maven-3.6.1-bin.tar.gz -C /opt
ls -lrt /opt

Sample Output:

[root@devopsio tmp]# sudo tar xf /tmp/apache-maven-3.6.1-bin.tar.gz -C /opt
[root@devopsio tmp]# ls -lrt /opt/
total 4
drwxr-xr-x. 6 root root 4096 Aug  5 15:04 apache-maven-3.6.1

We will create a symbolic link maven which will point to the Maven installation directory, to have more control over Maven versions and updates:
Enter command below:

sudo ln -s /opt/apache-maven-3.6.1 /opt/maven
ls -lrt /opt

Sample Output:

[root@devopsio opt]# ls -lrt /opt
total 4
drwxr-xr-x. 6 root root 4096 Aug  5 15:04 apache-maven-3.6.1
lrwxrwxrwx. 1 root root   23 Aug  5 15:07 maven -> /opt/apache-maven-3.6.1

Step 3. Setup Environment variables.

Next, we`ll need to setup the Environment variables.

1. Check directory path JDK installed.
Enter the command below:

which java

Sample Output:

[root@devopsio opt]# which java
/usr/bin/java

Next use the above results to find. Enter the command below:

ls -lrt /usr/bin/java

Sample Output:

[root@devopsio opt]# ls -lrt /usr/bin/java
lrwxrwxrwx. 1 root root 22 Aug  5 14:28 /usr/bin/java -> /etc/alternatives/java

Next use the above results to find. Enter the command below:

ls -lrt /etc/alternatives/java

[root@devopsio opt]# ls -lrt /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 Aug  5 14:28 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre/bin/java

Finally, We found the JDK directory path to be installed. [ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre/ ]

2. Next, you create the script. Enter the command below:

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

Paste the following lines:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Save the script file. After make the script executable.
Enter command below:

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

Load the environment variables using the source command:
Enter command below:

source /etc/profile.d/maven.sh

Step 4. Verify the installation.

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

mvn -version

Sample Output:

[root@devopsio opt]# mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T19:00:29Z)
Maven home: /opt/maven
Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

IV. Conclusion.

In this tutorial, we was install Apache Maven on CentOS 7 server. If you hit a problem or have feedback, leave a comment below.
You can now visit the official Apache Maven Documentation page and learn how to get started with Maven.


Install Maven on CentOS 7 is complete. Wish you success and luck.
Thanks and best regards!
— Pring —

Related Articles

Leave a Comment