fbpx

We will walk you through the process of installing Apache Maven on CentOS Linux. We will cover the installation of the necessary packages, configuring the environment variables, and demonstrate how to build projects using Maven. Apache Maven is a powerful build automation tool that plays a crucial role in managing Java projects.

Prerequisites:

Before we proceed, ensure that you have a CentOS Linux distribution installed on your system and have administrative privileges.

Step 1: Update the System

To begin, let’s update the system’s package repositories and installed packages. Open the terminal and run the following command:

sudo yum update

Step 2: Install Java Development Kit (JDK)

Apache Maven on CentOS requires Java to be installed on your system. We will install the OpenJDK package, which is a free and open-source implementation of the Java Development Kit. Enter the following command in the terminal:

sudo yum install java-1.8.0-openjdk-devel

Step 3: Downloading Apache Maven

Next, we need to download the latest stable version of Apache Maven on CentOS. Visit the official Apache Maven website (https://maven.apache.org/download.cgi) and locate the link to the Binary tar.gz archive. Copy the link address for the most recent release.

Back in the terminal, navigate to the directory where you want to store the Apache Maven on CentOS installation package and use the wget command to download it. Replace the link below with the copied URL:

# cd /opt
# wget https://dlcdn.apache.org/maven/maven-3/3.9.2/binaries/apache-maven-3.9.2-bin.tar.gz
# tar zxvf apache-maven-3.9.2-bin.tar.gz

Step 4: Configuring Environment Variables

To configure the environment variables for Apache Maven, we will create a new file called maven.sh in the /etc/profile.d/ directory. Run the following command:

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

Add the following lines to the file:

cat /etc/profile.d/maven.sh
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
export M2_HOME=/opt/apache-maven-3.9.2
export MAVEN_HOME=/opt/apache-maven-3.9.2
export PATH=${M2_HOME}/bin:${PATH}

We need to make the maven.sh script executable. Execute the following command:

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

Step 5: Apply Environment Variables

To apply the newly added environment variables, either restart your system or run the following command in the terminal:

source /etc/profile.d/maven.sh

To verify that Maven is correctly installed, enter the following command:

mvn -version

You should see the Maven version and other details printed on the console if the installation was successful.

Step 6: Building Projects with Maven

Now that Maven is installed, let’s create a sample Maven project to ensure everything is functioning correctly. Move to the directory where you want to create your project and run the following command:

# mvn archetype:generate -DgroupId=com.example -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command generates a basic Maven project structure with the specified group and artifact IDs.

How to install Apache Maven on CentOS 7

Step 7: Build and Run the Project

Navigate into the newly created project directory:

# cd myproject

Build the project using Maven:

# mvn package
How to install Apache Maven on CentOS 7

If the build is successful, you can execute the compiled project:

# java -cp target/myproject-1.0-SNAPSHOT.jar com.example.App
Hello World!

Congratulations! You have successfully installed Apache Maven on CentOS Linux and built your first project using Maven.

Final Thoughts

In this tutorial, we covered the step-by-step process of installing Apache Maven on CentOS Linux. We discussed the installation of necessary packages, configuring environment variables, and building a sample project with Maven. With Apache Maven, you now have a powerful tool at your disposal to efficiently manage your Java projects on CentOS Linux. Happy coding!


1 Comment

How To Install Apache Maven On Ubuntu - Virtono Community · September 9, 2023 at 2:05 PM

[…] Project Object Model (POM) concept serves as the foundation for Apache Maven, also known as Maven, which is a potent tool for managing and understanding software projects. The […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.