How 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 provides 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. 

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.

Install OpenJDK on AlmaLinux 9

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
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)

Set up Apache Maven on AlmaLinux 9

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

Download Maven

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
Output
Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /opt/maven
Java version: 1.8.0_352, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.352.b08-2.el9_1.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.14.0-70.22.1.el9_0.x86_64", arch: "amd64", family: "unix"

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

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:

Output
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.050 s
[INFO] Finished at: 2023-01-07T03:12:53-05:00
[INFO] ------------------------------------------------------------------------

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:

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 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
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

Let’s cat the default pom.xml file to see what it looks like.

sudo cat pom.xml
Output
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jcg.maven</groupId>
  <artifactId>MavenHelloWorldProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MavenHelloWorldProject</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

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.

Hope you enjoy it.

You may be like these articles:

How To Install AnyDesk on AlmaLinux 9

Install Python 3.11 on AlmaLinux 9

Install PHP 8.2 on AlmaLinux 9

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!