Installing Maven on Ubuntu

Installing Maven on Ubuntu

Apache Maven is an open-source project management tool primarily used to develop Java applications. It incorporates a POM (Project Object Model) approach, which stores information about projects, configurations, and dependencies in an XML file.

Step 1: Install OpenJDK

Update the system's package repository

sudo apt update

Install the latest OpenJDK version

sudo apt install default-jdk -y

Verify the installation by checking the current OpenJDK version:

java -version

Step 2: Download and Install Maven

Download the Maven installation file to the /tmp directory using the wget cmd

wget https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz -P /tmp

Once the download is complete, extract the installation file to the /opt directory:

sudo tar xf /tmp/apache-maven-3.9.8-bin.tar.gz -C /opt

Create a symbolic link leading to the Maven installation directory:

sudo ln -s /opt/apache-maven-3.9.8 /opt/maven

Step 3: Set Up Environment Variables

Create and open the maven.sh script file in the /etc/profile.d/ directory:

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

Add the following lines to the maven.sh file:

export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Use the chmod command to make the maven.sh file executable:

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

Execute the maven.sh script file with the source command to set up the new environment variables:

source /etc/profile.d/maven.sh

Step 4: Verify Maven Installation

mvn -version