Apache maven Installation and Configuration

How to Install and Configure Apache Maven on Ubuntu, CentOS, and Windows

Apache Maven is a build automation tool for Java projects. This tool is based on the Project Object Model (POM). Maven is very useful and powerful in terms of building Java projects, managing dependencies, and can be easily integrated with the source code management system. In this article, we will see how to install and configure Apache Maven on Ubuntu, CentOS, and Windows.

Install Maven on Ubuntu

Pre-requisite

  • Ubuntu machine (Here I tool 16.04)
  • Non- root user with ‘sudo’ privileges.

Step 1: Update Apt repositories

Put the following commands in the console.

sudo apt-get update
sudo apt-get upgrade

Step 2: Installing Java

We need to install java because maven needs java to be installed. Enter the following commands in the console.

sudo apt-get install default-jdk 

Verify the java installation by entering following command in the console.

java -version

This should through the version of the installed java.

Step 3: installing Apache maven

Download the maven binary code in /opt folder by entering the following command in the console

cd /opt/

There is multiple version of apache maven is available in the official website

wget http://us.mirrors.quenda.co/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz

Extract maven packages by entering the following command.

tar -xf apache-maven-3.6.1-bin.tar.gz

For the clear and safe communication, always keep binary files in the folder which name is in standard characters. In our case, we are selecting “apache-maven”

mv apache-maven-3.6.1/ apache-maven/

Step 4: Configure maven environment.

We need to set up the maven environment so that it will be available as an executable binary. For that, create maven.sh file in the /etc/profile.d folder by entering the following command

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

Add the follwong lines in the file.

export M2_HOME=/opt/apache-maven
export MAVEN_HOME=/opt/apache-maven
export PATH=${M2_HOME}/bin:${PATH}

Save the file and close by pressing ctrl + x then y then enter.

Now Make the maven.sh executable by entering the following command

chmod +x maven.sh

Then, publish the configuration by sourcing the file by following command.

source maven.sh

That  all the setup, test the maven setup by checking the version by entering the following command

mvn –version

This should give the version of the maven installed.

So installation of Apache maven on Ubuntu is done.

Install Apache Maven on CentOS

Pre-requisite

  • CentOs machine (Here I tool CentOS7)
  • Non- root user with ‘sudo’ privileges.

Step 1: Install Java

As we already said, Apache Maven needs the jdk. So, we need to install Java first. Install Java by entering the following command.

sudo yum install java-1.8.0-openjdk-devel

Then, Verify the java installation by entering the following command.

Java -version

Step 2: Download the Maven binary

Download the maven binary code in /opt folder by entering the following command in the console

cd /opt/

There is multiple version of apache maven is available on the official website. Download the version you want by entering the following command.

wget http://us.mirrors.quenda.co/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz

Note: If you do not have ‘wgetinstalled, get it downloaded first by entering the following command

sudo yum install wget

Then extract the file in the folder.

tar -xf apache-maven-3.6.1-bin.tar.gz

then move the binary files to the standard files.

mv apache-maven-3.6.1/ apache-maven/

Step 3: Configure the maven environment

Just like Ubuntu, we need to configure the maven environment just like how we configured in Ubuntu.

So, follow the same steps in the Ubuntu configuration.

Step 4: Verify the maven installation

Now, Verify the maven installation by entering the following command.

mvn –version

So, this will give the version of the maven installed.

Installing Maven in Windows.

Pre-requisite

  • Windows machine (Here I tool Windows 10)
  • Admin Privilege.

Step 1: Install Java

Installing Java on windows is straight forward.

Follow the steps in this wonderful article about how to install java on windows

Step 2: Download Maven.

As we already said. There are multiple versions of apache maven is available on the official website. Then, Download the needed version as a zip file.

Apache Maven Download

Step 3: Extract Maven

Now, Extract the downloaded maven binary package to any location. In our case, I am choosing a root folder of c drive. (C:\maven)

Maven Binary

Step 4: Set Maven environment

We need to create environment variable by following steps.

  1. Right click “This PC” and select “Properties”.
  2. This will open a window called “System Properties”. In that Click “Advanced” Tab.
  3. Then Select “Environment variable”.
  4. This will open a new window and then Under “System Variables” select “New”.
  5. Again, another new window called “New System variable”.
  6. Then enter “Variable Name” as “M2_HOME” and enter “variable Value” as “C:\maven”.
  7. Select “Path” in “System Variable” and click “Edit” button.
  8. A New window will open and enter the binary path of maven(“M2_HOME/bin”). In our case, “C:\maven\bin”.

Once Done, Save and close the window. To verify the installation and configuration, Enter the following command in Command prompt.

mvn –version

This should show the version of maven that you have installed.

Conclusion

We have seen how to install and configure Maven on Ubuntu, CentOS, and Windows. We will see more How to’s and more articles about DevOps, DevSecOps, Agile and App Development in the future. Stay tuned and subscribe DigitalVarys for the updates on new Articles.

1 thought on “How to Install and Configure Apache Maven on Ubuntu, CentOS, and Windows”

Leave a Reply