Introduction
Apache Maven is a popular open source build automation tool used for Java projects. It is used to manage the project’s build, dependency, and documentation. Installing Apache Maven on CentOS 7 is a straightforward process that can be completed in a few simple steps. This guide will walk you through the process of installing Apache Maven on CentOS 7.
How to Install Apache Maven on CentOS 7
1. First, update the system packages to the latest version:
sudo yum update
2. Install Java Development Kit (JDK)
sudo yum install java-1.8.0-openjdk
3. Download the latest version of Apache Maven from the official website:
wget http://www-eu.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
4. Extract the downloaded archive:
tar xzf apache-maven-3.6.3-bin.tar.gz
5. Move the extracted folder to the /opt directory:
sudo mv apache-maven-3.6.3 /opt
6. Create a symbolic link to the Maven folder:
sudo ln -s /opt/apache-maven-3.6.3/ /opt/maven
7. Add the Maven bin directory to the PATH environment variable:
export PATH=/opt/maven/bin:$PATH
8. Verify the installation by running the mvn -v command:
mvn -v
You should see the following output:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
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-1062.el7.x86_64”, arch: “amd64”, family: “unix”
Introduction
Apache Maven is a project management tool for developing Java applications designed to provide a comprehensive and easy-to-read status of a project.
It also incorporates a POM (Project Object Model) approach, meaning that it uses standardized software libraries and plugins. These features help create a standard development environment for multiple teams.
This tutorial will walk you through installing Apache Maven on CentOS 7.
Prerequisites
- A system running CentOS 7
- Access to a user account with sudo or root privileges
- Access to a terminal window/command-line (Ctrl-Alt-F2)
- The yum package manager, included by default
- (Optional) The wget software utility
Option 1: Install Apache Maven on CentOS with Yum
1. Open a terminal window, and enter the following:
sudo yum install maven
Confirm the installation, and allow the process to complete. Maven should now be installed on your system.
2. Verify the installation by checking the Maven version:
mvn ––version
The following output confirms a successful install:
Note: At the time this article was written, the latest stable version of Apache Maven was 3.6.0. If you need an older version, you can check Maven’s release history.
Option 2: Install Latest Version of Maven
Sometimes the software version in the default repositories is older than the latest stable release from the developers. This section will walk you through the manual installation of Apache Maven on CentOS.
Step 1: Install Java Development Kit
Apache Maven requires the Java Development Kit. This guide uses the OpenJDK software package.
1. In a terminal window, enter the following:
sudo yum install java-1.8.0-openjdk-devel
Allow the process to complete.
2. Verify the installation by checking the Java version:
java –version
Step 2: Download Apache Maven for CentOS
Download the Maven source file to the /tmp directory with the command:
sudo wget http://mirrors.ibiblio.org/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz –P /tmp
If you get an error “command not found,” you may be missing the wget software. Install it by entering:
sudo yum install wget
Extract the .tar.gz archive:
sudo tar xf /tmp/apache-maven-2.6.0-bin.tar.gz –C /opt
(Optional) Create a symbolic link by entering:
sudo ln –s /opt/apache-maven-3.6.0 /opt/maven
This step preserves the extracted directory but allows you to use /opt/maven as a shortcut to that directory.
Step 3: Configure Environment
Apache Maven works from a configuration file, /etc/profile.d/maven.sh.
1. Create and edit the configuration file by entering:
sudo vi /etc/profile.d/maven.sh
2. Enter the following lines:
# Apache Maven Environmental Variables
# MAVEN_HOME for Maven 1 - M2_HOME for Maven 2
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Save the file and exit.
3. Modify the file permissions with the chmod command by entering the following:
sudo chmod +x /etc/profile.d/maven.sh
4. Then load the file with the command:
source /etc/profile.d/maven.sh
Step 4: Verify Apache Maven Installation
To verify the installation of Apache Maven, use the command:
mvn ––version
The system should display Apache Maven 3.6.0 and Maven Home: /opt/maven.
Using Apache Maven
Maven was designed initially for Java projects. However, it can also be used to build and manage other programming languages, such as Ruby and C#.
One of the ways that Maven works is through the use of plugins. Maven can download libraries and plugins from a central repository and cache a local version. Most plugins are written to support Java. If you’re planning to use Maven to manage another programming language, you may need to write your own plugins for that language.
Conclusion
Great job, you have successfully installed Apache Maven on CentOS 7.
For more information about installation, or about beginner and intermediate tutorials, please check out the official Maven documentation.
How to Install Apache Maven on CentOS 7
Apache Maven is a popular open source build automation tool used primarily for Java-based projects. It is used to manage dependencies, build, test, and deploy applications. In this tutorial, we will show you how to install Apache Maven on CentOS 7.
Prerequisites
- A server running CentOS 7.
- A root user or a user with sudo privileges.
Step 1: Update the System
Before starting, it is recommended to update the system packages to the latest version. You can do this by running the following command:
sudo yum update -y
Step 2: Install Java
Apache Maven requires Java to be installed on the system. You can install Java by running the following command:
sudo yum install java-1.8.0-openjdk -y
Step 3: Download Apache Maven
You can download the latest version of Apache Maven from its official website. At the time of writing this article, the latest version is 3.6.3. You can download it with the following command:
wget http://www-eu.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
Step 4: Install Apache Maven
Once the download is completed, extract the downloaded file with the following command:
tar -xzvf apache-maven-3.6.3-bin.tar.gz
Next, move the extracted directory to the /opt directory:
sudo mv apache-maven-3.6.3 /opt
Next, create a symbolic link of the maven directory with the following command:
sudo ln -s /opt/apache-maven-3.6.3 /opt/maven
Step 5: Setup Environment Variables
Next, you will need to setup environment variables for Apache Maven. You can do this by creating a maven.sh file in the /etc/profile.d/ directory:
sudo nano /etc/profile.d/maven.sh
Add the following lines:
export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Save and close the file, then apply the changes with the following command:
source /etc/profile.d/maven.sh
Step 6: Verify the Installation
At this point, Apache Maven is installed and configured on your CentOS 7 system. You can verify the installation with the following command:
mvn -v
You should see the following output:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/maven
Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jre-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-1062.el7.x86_64", arch: "amd64", family: "unix"
Congratulations! You have successfully installed Apache Maven on your CentOS 7 server.