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.
Table of Contents
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 ‘wget‘ installed, 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.
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)
Step 4: Set Maven environment
We need to create environment variable by following steps.
- Right click “This PC” and select “Properties”.
- This will open a window called “System Properties”. In that Click “Advanced” Tab.
- Then Select “Environment variable”.
- This will open a new window and then Under “System Variables” select “New”.
- Again, another new window called “New System variable”.
- Then enter “Variable Name” as “M2_HOME” and enter “variable Value” as “C:\maven”.
- Select “Path” in “System Variable” and click “Edit” button.
- 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.
Certified Cloud Automation Architect and DevSecOps expert, skilled in optimizing IT workflows with Six Sigma and Value Stream Management. Proficient in both technical and leadership roles, I deliver robust solutions and lead teams to success.
Thank you for the amazing article