Easy Steps To Install Apache Maven on AlmaLinux 9

In this guide, we want to teach you How To Install Apache Maven on AlmaLinux 9. Maven is a popular open-source build tool developed by the Apache Group to build, publish, and deploy several projects at once for better project management. The tool allows developers to build and document the lifecycle framework. 

Maven is written in Java and is used to build projects in C#, Scala, Ruby, etc. Based on the Project Object Model (POM), this tool has made the lives of Java developers easier while developing reports, checks building, and testing automation setups. 

You can now proceed to the guide steps below on the Orcacore website to set up Apache Maven on AlmaLinux 9.

Steps To Install Apache Maven on AlmaLinux 9

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with AlmaLinux 9.

1. Install OpenJDK For Apache Maven Setup

As you know Maven is written in Java, so you need to have Java installed on your server. First, update your local package index with the following command:

sudo dnf update -y

Maven 3.+ requires JDK 1.7 or above. Use the command below to install the OpenJDK package on AlmaLinux 9:

sudo dnf install java-1.8.0-openjdk -y

At this point, Verify your Java installation by checking its version:

java -version
Install OpenJDK For Apache Maven Setup

2. Apache Maven Setup on AlmaLinux 9

In this guide, we will download and install the latest Maven. So follow the steps below to complete your installation.

Apache Maven Download

You need to visit the Apache Maven downloads page and copy the link address of the binaries package. Next, use the wget command to download it on your server in 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, you need to extract your downloaded file in the opt directory:

sudo tar xf /tmp/apache-maven-3.8.7-bin.tar.gz -C /opt

Now you should create a symbolic link that will point to the Maven installation directory on AlmaLinux 9:

sudo ln -s /opt/apache-maven-3.8.7 /opt/maven

Note: To upgrade your Maven installation, simply unpack the newer version and change the symlink to point to it.

Set up Maven Environment Variables

At this point, you should set up the Maven environment variables on AlmaLinux 9. To do this, use your favorite text editor to create a new file inside the /etc/profile.d directory:

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}

When you are done, save and close the file.

Set Correct Permissions For Maven File

Here you need to make your file executable with the following command:

sudo chmod +x /etc/profile.d/maven.sh

Load Maven Variables

At this point, you need to load the Maven environment variables on AlmaLinux 9 with the following command:

source /etc/profile.d/maven.sh

At this point, you can verify your Maven installation by checking its version:

mvn -version
Load Maven Variables

That’s it. The latest version of Apache Maven is now installed on your AlmaLinux system.

3. Test Maven Installation on AlmaLinux 9

At this point, we want to show you how to create a test program “Hello World” with your Maven. First, you need to create the project folder structure via the Maven command line:

mvn archetype:generate -DgroupId=com.jcg.maven -DartifactId=MavenHelloWorldProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

When it is completed, you will get the following output:

Test Maven Installation on AlmaLinux 9

To see the Maven project folders created go to the directory path from your output. Switch to the directory, and run the command below:

ls -l

You will get something similar to this:

Maven project folders

The output shows that two different directory structures are generated along with maven’s default pom.xml.

The source code of the application goes to src/main/java while our unit-test code goes to src/test/java.

# cd src
# ls-l
source code of the application

Let’s look at the default pom.xml file.

sudo cat pom.xml
default pom.xml file

From the output, the dependency section defines the libraries that the project will rely on. This is where the jar files go in.

Compile the Project with Maven

At this point, we will use Maven to compile the project in order to generate a JAR file on AlmaLinux 9. In our pom.xml file above the packaging element defined should be the packaging output. Since JAR is our packaging element as shown <packaging>jar</packaging> our output should be a JAR file.

Run the command below on the directory where your project files exist to compile your project:

mvn clean install

When it is completed, check the project directory \target folder to see the jar file created:

ls -l

Conclusion

At this point, you have learned to Install Apache Maven on AlmaLinux 9 and test your Maven installation. Apache Maven in AlmaLinux 9 is used for managing Java projects by automating builds, dependencies, and documentation. It simplifies project configuration and ensures consistency across development environments.

Hope you enjoy it. You may also like these articles:

How To Install AnyDesk on AlmaLinux 9

Install Python 3.11 on AlmaLinux 9

Install PHP 8.2 on AlmaLinux 9

Share your love

Stay informed and not overwhelmed, subscribe now!