Introduction:
Apache Maven is a widely used build automation and dependency management tool for Java-based projects. It simplifies the process of building and managing projects by providing a structured way to define project configurations. In this blog, we will guide you through the step-by-step process of installing Apache Maven on Linux systems.
Prerequisites:
Before proceeding with the Maven installation, make sure you have Java 8 or a higher version installed on your Linux system.
Step 1: Download Apache Maven
- Go to the Maven Downloads page at https://maven.apache.org/download.cgi
- Find the link to the latest Maven binary package and copy its URL. For this example, we will use version 3.9.3.
- Open a terminal or SSH into your Linux machine.
Step 2: Download and Extract Maven
- In the terminal, use the
wget
command to download the Maven binary package.
$ wget https://dlcdn.apache.org/maven/maven-3/3.9.3/binaries/apache-maven-3.9.3-bin.tar.gz
- Once the download is complete, extract the Maven package to the /opt folder using the
tar
command.
$ sudo tar -xvzf apache-maven-3.9.3-bin.tar.gz -C /opt
Step 3: Create a Symbolic Link
- Create a symbolic link to the Maven folder in the /opt directory to easily switch to new Maven versions in the future.
$ sudo ln -s /opt/apache-maven-3.9.3 /opt/maven
Step 4: Set Maven Environment Variables
- To make the
mvn
command available system-wide, we need to set the M2_HOME environment variable and add Maven’s bin directory to the system PATH. - Create a new script file named ‘maven.sh’ in the ‘profile.d’ folder using a text editor.
$ sudo vim /etc/profile.d/maven.sh
- Add the following lines to the ‘ maven.sh’ script and save the file.
export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
- Add execute permission to the’maven.sh’ script to make it executable.
$ sudo chmod +x /etc/profile.d/maven.sh
- Source the script to apply the changes immediately without restarting the terminal.
$ source /etc/profile.d/maven.sh
Step 5: Verify Maven Installation
- To verify that Maven is installed correctly, run the following command:
$ mvn -version
Conclusion:
Congratulations! You have successfully installed Apache Maven on your Linux system. Now you can use Maven to build and manage your Java-based projects efficiently.
By following the steps in this blog, you have set up Maven for system-wide access, allowing you to easily switch to new Maven versions in the future.