In this blog, you will learn to install maven on different platforms and learn about maven configurations using step-by-step guides.
Apache maven is one of the best open-source java build tools. It is primarily used by developers working on java projects and devops engineers working on CI/CD pipelines. A developer primarily deals with the development aspects using maven while devops engineers deal with setting up a Continuous Integration pipeline using maven.
Let’s get started with the maven setup.
Install Latest JDK
The basic prerequisite for Maven is Java JDK because Maven is a build tool and it requires all the build and compiles related java tools.
Let’s install the latest JDK.
For Ubuntu,
sudo apt install openjdk-17-jdk openjdk-17-jre -y
For Amazon Linux,
sudo yum install java-17-amazon-corretto-devel -y
For Redhat Linux,
yum install java-17-openjdk-devel -y
Verify Java JDK installation using the following command.
java -version
Install Maven (Latest From Maven Repo)
Follow the steps given below to install the latest maven package from the official maven repo.
Step 1: Go to Maven Downlaods and get the download link of the latest package.
wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
Step 2: Untar the mvn package to the /opt
folder.
sudo tar xvf apache-maven-3.9.9-bin.tar.gz -C /opt
Step 3: Create a symbolic link to the maven folder. This way, when you have a new version of maven, you just have to update the symbolic link and the path variables remain the same.
sudo ln -s /opt/apache-maven-3.9.9 /opt/maven
Note: Always install specific version of maven that is used across CI environment. So that there wont be any build issues due to version mismatch when you take the code from development to deployment environments
Add Maven folder to System PATH
To access the mvn command systemwide, you need to either set the M2_HOME
environment variable or add /opt/maven
to the system PATH
.
We will do both by adding them to the profile.d
folder. So that every time the shell starts, it gets sourced and the mvn command will be available system-wide.
Step 1: Create a script file named maven.sh
in the profile.d
folder.
sudo vi /etc/profile.d/maven.sh
Step 2: Add the following to the script and save the file.
export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Step 3: Add execute permission to the maven.sh
script.
sudo chmod +x /etc/profile.d/maven.sh
Step 4: Source the script for changes to take immediate effect.
source /etc/profile.d/maven.sh
Step 5: Verify maven installation
mvn -version
Note: In Ubuntu based systems, you can also add
M2_HOME
andPATH
variables to users .bashrc file as well.
Maven Important Configurations
Following are the important maven configurations you should know.
Configuration | Location | Details |
---|---|---|
M2_HOME environment variable | The location is the corresponding maven installation directory in the system. | |
.m2 local repository | $HOME/.m2 | When you run maven commands, all the remote repositories get cached locally in the .m2 folder. |
settings.xml (Global) | $M2_HOME/conf/settings.xml | This file contains all the maven configurations that are common for all the projects. |
settings.xml (User Specific) | $HOME/.m2/settings.xml |
Validate Maven Using Java Build
To validate the maven installation, you can start by building a Java application.
If you have a java application with pom.xml
file, just run the following command.
mvn clean install
If you don’t have a java application, you can follow the building Java application using maven detailed guide.
Conclusion
When it comes to CI/CD, maven is part of java build agents. When installing maven on build agents ensure a specific version of maven that supports the installed JDK version.
Also, Maven is a must-know tool for DevOps engineers in terms of Continuous integration. Check out the guide to become a devops engineer to know more.
1 comment
That was clean and very good explanation..