Install and Configure Apache Maven on Rocky Linux 8
This guide provides a comprehensive walkthrough on how to Install and Configure Apache Maven on Rocky Linux 8. Maven is a powerful and versatile project management tool built around the Project Object Model (POM). It streamlines project builds, manages dependencies efficiently, and facilitates comprehensive documentation. Think of it as an advanced version of ANT, offering a more sophisticated and streamlined approach to Java project management.
In essence, Maven is a valuable asset for building and managing Java-based projects of any scale. It simplifies the daily tasks of Java developers and promotes a clearer understanding of project structure and dependencies.
Let’s delve into the steps necessary to Install and Configure Apache Maven on Rocky Linux 8, as detailed on the Orcacore website.
Before proceeding, ensure you are logged in to your Rocky Linux 8 server as a non-root user with sudo privileges. You can refer to the "Initial Server Setup with Rocky Linux 8" guide for assistance with this.
We will be downloading the latest Apache Maven release directly from its official website.
1. Install OpenJDK for Maven
Maven 3.3 and later versions require Java Development Kit (JDK) 1.7 or higher.
Begin by updating your local package index:
sudo dnf update -y
Next, install the OpenJDK package using the following command:
sudo dnf install java-1.8.0-openjdk -y
Verify the Java installation by checking its version:
java -version
2. Install Maven on Rocky Linux 8
Visit the Apache Maven downloads page and copy the link address for the binaries package.
Download Maven
Use the wget
command to download the Maven binaries to the /tmp
directory:
sudo wget https://dlcdn.apache.org/maven/maven-3/3.8.7/binaries/apache-maven-3.8.7-bin.tar.gz -P /tmp
Extract the downloaded file to the /opt
directory:
sudo tar xf /tmp/apache-maven-3.8.7-bin.tar.gz -C /opt
Create Symbolic Link for Maven
Create a symbolic link pointing to the Maven installation directory:
sudo ln -s /opt/apache-maven-3.8.7 /opt/maven
Note: To upgrade your Maven installation, simply unpack the newer version and update the symlink to point to it.
Set up Maven Environment Variables
Configure the Maven environment variables. Create a new file in the /etc/profile.d
directory using your preferred text editor:
sudo vi /etc/profile.d/maven.sh
Add the following content to the file:
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
Save and close the file.
Make the file executable:
sudo chmod +x /etc/profile.d/maven.sh
Load the Maven environment variables:
source /etc/profile.d/maven.sh
Verify the Maven installation by checking its version:
mvn -version
The latest version of Apache Maven is now installed on your Rocky Linux 8 system.
Conclusion
You have successfully learned how to Install and Configure Apache Maven on Rocky Linux 8. Maven is instrumental in managing Java projects by automating builds, dependency resolution, and project lifecycles, significantly simplifying the process of compiling, testing, and packaging Java applications.
Here are some additional articles you might find useful:
- Fix PuTTY Server Refused Our Key Error
- Install Postman on Rocky Linux 8
- Install Python 3.11 on Rocky Linux 8
- How To Install Ntopng on Rocky Linux 9
- Run Postman on Rocky Linux 8
- How To Install Joomla With LAMP Stack On Rocky Linux 9
- Download and install Zabbix for Rocky Linux 9
- How to Install PHP 8.2 in Rocky Linux 9
Alternative Solutions to Installing Apache Maven on Rocky Linux 8
While the above method is a standard approach, here are two alternative methods to Install and Configure Apache Maven on Rocky Linux 8:
1. Using SDKMAN! (Software Development Kit Manager)
SDKMAN! is a versatile tool for managing multiple software development kits (SDKs) on Unix-like systems. It simplifies the installation, management, and switching between different versions of SDKs, including Java, Maven, Gradle, and more.
Steps:
-
Install SDKMAN!:
First, install SDKMAN! using the following command:
curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions to complete the installation. You’ll likely need to open a new terminal or source your shell configuration file (e.g.,
source ~/.bashrc
orsource ~/.zshrc
). -
Install Maven using SDKMAN!:
Once SDKMAN! is installed, you can install Maven with a single command:
sdk install maven
SDKMAN! will download and install the latest stable version of Maven.
-
Verify Maven Installation:
Verify the installation by checking the Maven version:
mvn -version
SDKMAN! automatically manages the environment variables, so you don’t need to set them manually.
-
Installing a Specific Maven Version:
To install a specific version of Maven, you can first list the available versions using:
sdk list maven
Then install your chosen version:
sdk install maven <version>
For example:
sdk install maven 3.8.6
Explanation:
SDKMAN! provides a convenient and automated way to install and manage Maven. It handles the downloading, extracting, and environment variable configuration, simplifying the process significantly. It also allows you to easily switch between different Maven versions if needed.
Code Example:
# Install SDKMAN!
curl -s "https://get.sdkman.io" | bash
# Open a new terminal or source your shell config
source "$HOME/.sdkman/bin/sdkman-init.sh"
# Install Maven
sdk install maven
# Verify Maven version
mvn -version
2. Using a Package Manager (if available)
While Maven is not directly available in the default Rocky Linux 8 repositories, some third-party repositories might offer it. Warning: This is less common and can introduce dependency conflicts or security risks if the repository is not well-maintained. Proceed with caution. I highly recommend avoiding this option.
Disclaimer: This method is not recommended due to potential security risks associated with using untrusted repositories.
Steps (If a Repository Exists):
-
Enable the Repository (Hypothetical):
Assume there’s a repository called "example-repo" that provides Maven. You would enable it (the exact command depends on the repository):
sudo dnf config-manager --add-repo example-repo
(Replace "example-repo" with the actual repository name.)
-
Install Maven:
Use
dnf
to install Maven:sudo dnf install maven
-
Verify Maven Installation:
Check the Maven version:
mvn -version
Explanation:
If a repository provides a Maven package, using dnf
simplifies the installation process. However, this method is less reliable because it depends on the availability and trustworthiness of third-party repositories. Always prioritize security and only use trusted repositories.
Code Example (Hypothetical):
# This is a hypothetical example and may not work
# Only use trusted repositories
# sudo dnf config-manager --add-repo example-repo # Replace with actual repo
# sudo dnf install maven
# mvn -version
Important Considerations:
- Security: Always prioritize security when adding third-party repositories. Research the repository’s reputation and ensure it’s actively maintained.
- Dependencies: Using a package manager might install dependencies automatically, but it’s still essential to understand the dependencies involved.
- Version Control: SDKMAN! provides better version control compared to relying solely on a package manager.
In conclusion, while installing via direct download and configuration remains a reliable method to Install and Configure Apache Maven on Rocky Linux 8, SDKMAN! offers a more streamlined and versatile approach. The package manager approach should be avoided due to security risks unless you fully trust the repository.