Install Apache Maven on Rocky Linux 9 with Easy Steps
This guide will walk you through the process to Install Apache Maven on Rocky Linux 9. Maven is a powerful project management tool based on the Project Object Model (POM). It’s primarily used for project building, dependency management, and documentation. Maven simplifies the build process, offering significant advancements over tools like ANT.
In essence, Maven is a tool designed for building and managing Java-based projects. It streamlines the daily tasks of Java developers and aids in understanding complex Java projects.
Follow these steps to Install Apache Maven on Rocky Linux 9, brought to you by Orcacore.
Before you begin to Install Apache Maven on Rocky Linux 9, ensure you are logged into your server as a non-root user with sudo privileges. You can refer to our guide on Initial Server Setup with Rocky Linux 9 for assistance.
1. Install OpenJDK on Rocky Linux 9
Since Maven is written in Java, you need to have Java installed on your server.
First, update your local package index:
sudo dnf update -y
Maven 3.+ requires JDK 1.7 or above. 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
**Output**
openjdk version "1.8.0_352"
OpenJDK Runtime Environment (build 1.8.0_352-b08)
OpenJDK 64-Bit Server VM (build 25.352-b08, mixed mode)
2. Set up Apache Maven on Rocky Linux 9
This guide will focus on installing the latest version of Maven. Follow these steps:
Download Maven
Visit the Apache Maven downloads page and copy the link address of the binaries package. Use the wget
command to download Maven 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
Next, extract the downloaded file to the /opt
directory:
sudo tar xf /tmp/apache-maven-3.8.7-bin.tar.gz -C /opt
Create a symbolic link to point 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 symbolic link.
Set up Maven Environment Variables
Configure the Maven environment variables. Create a new file inside the /etc/profile.d
directory using your favorite 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.
Set Correct Permissions For Maven File
Make the file executable:
sudo chmod +x /etc/profile.d/maven.sh
Load Maven Variables
Load the Maven environment variables:
source /etc/profile.d/maven.sh
Verify the Maven installation:
mvn -version
Apache Maven is now installed on your Rocky Linux system.
3. Test Maven Installation on Rocky Linux 9
Create a test "Hello World" program using Maven. First, generate the project folder structure:
mvn archetype:generate -DgroupId=com.jcg.maven -DartifactId=MavenHelloWorldProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Output:
Navigate to the Maven project folders.
Change to the created project directory and list the contents:
ls -l
**Output**
total 8
-rw-r--r-- 1 root root 670 Jan 7 03:12 **pom.xml**
drwxr-xr-x 4 root root 4096 Jan 7 03:12 **src**
The output shows the directory structure and the default pom.xml
file.
The application source code resides in src/main/java
, and the unit test code resides in src/test/java
.
# cd src
# ls-l
**Output**
total 8
drwxr-xr-x 3 root root 4096 Jan 7 03:12 **main**
drwxr-xr-x 3 root root 4096 Jan 7 03:12 **test**
Display the contents of the default pom.xml
file:
sudo cat pom.xml
The pom.xml
file defines the project’s dependencies.
Compile the Project with Maven
Compile the project to generate a JAR file. The pom.xml
file specifies the packaging element. Since <packaging>jar</packaging>
is defined, the output will be a JAR file.
Run the following command in the project directory:
mvn clean install
After completion, check the target
folder to see the created JAR file:
ls -l target
Conclusion
You have now learned to Install Apache Maven on Rocky Linux 9 and tested your installation.
Alternative Solutions to Installing Apache Maven on Rocky Linux 9
While the above method details a manual installation process, here are two alternative approaches to Install Apache Maven on Rocky Linux 9 that offer different benefits:
1. Using SDKMAN! (Software Development Kit Manager)
SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits (SDKs) on Unix-like systems. This includes Java, Maven, Gradle, and other related tools. Using SDKMAN! provides a simplified installation and version management experience.
-
Explanation: SDKMAN! handles the download, installation, and configuration of Maven. It also allows you to easily switch between different Maven versions if needed. This is especially useful for developers working on multiple projects with varying Maven requirements.
-
Steps:
a. Install SDKMAN!:
curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions to complete the SDKMAN! installation. You’ll likely need to open a new terminal or source the SDKMAN! initialization script:
source "$HOME/.sdkman/bin/sdkman-init.sh"
b. Install Maven using SDKMAN!:
sdk install maven
SDKMAN! will download and install the latest stable version of Maven.
c. Verify Installation:
mvn -version
SDKMAN! also allows you to install specific Maven versions:
sdk install maven 3.8.6 # Install Maven version 3.8.6 sdk use maven 3.8.6 # Use Maven version 3.8.6 in the current shell
2. Using a Container (Docker)
Another robust alternative is to use Docker to containerize Maven. This approach eliminates dependencies on the host system and provides a consistent environment for building Java projects.
-
Explanation: Docker packages Maven and its dependencies into a container image. This ensures that Maven runs in a predictable and isolated environment, regardless of the host operating system. This is beneficial for reproducibility and portability, especially in CI/CD pipelines.
-
Steps:
a. Install Docker: Follow the official Docker documentation for installing Docker on Rocky Linux 9.
b. Pull the Maven Docker Image:
docker pull maven:latest
This command downloads the latest official Maven Docker image from Docker Hub. You can also specify a specific Maven version, such as
maven:3.8.7
.c. Run Maven in a Container:
To execute Maven commands, you’ll need to run a container from the image. The following command mounts your project directory into the container, allowing Maven to access your project files:
docker run --rm -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:latest mvn clean install
--rm
: Automatically removes the container after it exits.-v "$(pwd)":/usr/src/mymaven
: Mounts the current working directory (your project directory) to the/usr/src/mymaven
directory inside the container.-w /usr/src/mymaven
: Sets the working directory inside the container to/usr/src/mymaven
.maven:latest
: Specifies the Docker image to use.mvn clean install
: The Maven command to execute.
d. Create an Alias (Optional):
For convenience, you can create an alias to simplify running Maven commands within a Docker container:
alias mvn="docker run --rm -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:latest mvn"
Now you can use the
mvn
command directly, and it will execute within the Docker container. For example:mvn clean install
These alternative methods provide flexible ways to Install Apache Maven on Rocky Linux 9 depending on your specific needs and environment. SDKMAN! offers simplified version management, while Docker provides a consistent and isolated build environment.