Latest

Saturday, 3 December 2022

How to install Apache Maven on Rocky Linux 9

How to install Apache Maven on Rocky Linux 9

In this configuration guide, you will learn how to install Apache Maven on Rocky Linux 9 and other Red Hat based Linux distros.

 

Table of Contents:

 

What is Apache Maven?:

Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

Maven addresses two aspects of building software: how software is built and its dependencies. Unlike earlier tools like Apache Ant, it uses conventions for the build procedure. Only exceptions need to be specified. An XML file describes the software project being built, its dependencies on other external modules and components, the build order, directories, and required plug-ins. It comes with pre-defined targets for performing certain well-defined tasks such as compilation of code and its packaging. Maven dynamically downloads Java libraries and Maven plug-ins from one or more repositories such as the Maven 2 Central Repository, and stores them in a local cache. This local cache of downloaded artifacts can also be updated with artifacts created by local projects. Public repositories can also be updated.

Maven is built using a plugin-based architecture that allows it to make use of any application controllable through standard input. A C/C++ native plugin is maintained for Maven 2.

 

Environment Specification:

We are using a minimal installed Rocky Linux 9 virtual machine with following specifications.

  • CPU - 3.4 Ghz (2 cores)
  • Memory - 2 GB
  • Storage - 20 GB
  • Operating System - Rocky Linux release 9.0 (Blue Onyx)
  • Hostname - maven-01.centlinux.com
  • IP Address - 192.168.116.133/24

 

Prepare Your Linux Server for Maven:

By using a ssh client, connect with your Rocky Linux server as root user.

Use hostnamectl command to set a FQDN (Fully Qualified Domain Name) for your server.

# hostnamectl set-hostname maven-01.centlinux.com

Build cache for your enabled yum repositories.

# dnf makecache
Rocky Linux 9 - BaseOS                          647  B/s | 3.6 kB     00:05
Rocky Linux 9 - AppStream                       384  B/s | 4.1 kB     00:10
Rocky Linux 9 - Extras                          1.1 kB/s | 2.9 kB     00:02
Metadata cache created.

Execute following command to update software packages in your Linux operating system.

# dnf update -y

Sometimes the above command also updates the Linux Kernel. Therefore, you should reboot your Linux server before moving forward with this configuration guide.

# reboot

After reboot, verify the Linux operating system and Kernel versions.

# cat /etc/rocky-release
Rocky Linux release 9.1 (Blue Onyx)

# uname -r
5.14.0-162.6.1.el9_1.0.1.x86_64

 

Installing Apache Maven from Yum Repository:

Apache Maven is available in standard yum repositories of Red Hat based Linux distros.

Therefore, you can easily install it by executing dnf command.

# dnf install -y maven

After installation check the version of Apache Maven software and Java runtime environment (The preferred version of Java is automatically installed by above command as a dependency).

# mvn --version
Apache Maven 3.6.3 (Red Hat 3.6.3-14)
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.17.0.8-2.el9_0.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.14.0-162.6.1.el9_1.0.1.x86_64", arch: "amd64", family: "unix"

# java --version
openjdk 11.0.17 2022-10-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS, mixed mode, sharing)

Your Build Automation Tool: Apache Maven is successfully installed.

But you may notice that the installed version of Apache Maven is not the latest version.

It is because of the fact that, the Linux standard yum repositories are updated periodically by the software vendors.

Now, the question is how to install the latest version of Apache Maven? that is not available in standard yum repositories.

 

Install Apache Maven from Official Binary tar.gz archive:

Provision a fresh Rocky Linux server. (Do not use the Linux server where you have installed Apache Maven from Yum repository in previous section)

As per Maven Official website "Maven 3.3+ require JDK 1.7 or above to execute".

Therefore, you may install that the latest version of Java available in standard yum repositories.

Along with Java, you should also install wget and tar software packages that will be used to download and extract the Apache Maven binary tar.gz archive.

# dnf install -y java-17-openjdk wget tar

After installation, verify the version of active Java runtime environment.

# java --version
openjdk 17.0.5 2022-10-18 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.5.0.8-2.el9_0) (build 17.0.5+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.5.0.8-2.el9_0) (build 17.0.5+8-LTS, mixed mode, sharing)

Download latest version of build automation software by executing following command. (You may find the URL of latest Binary tar.gz archive from Apache Maven website.)

# cd /tmp
# wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
--2022-12-01 08:33:14--  https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8676320 (8.3M) [application/x-gzip]
Saving to: ‘apache-maven-3.8.6-bin.tar.gz’

apache-maven-3.8.6- 100%[===================>]   8.27M  1.04MB/s    in 7.3s

2022-12-01 08:33:23 (1.13 MB/s) - ‘apache-maven-3.8.6-bin.tar.gz’ saved [8676320/8676320]

After download, extract it into /opt directory as follows.

# tar xf apache-maven-3.8.6-bin.tar.gz -C /opt

List /opt directory by using ls command.

# ls /opt
apache-maven-3.8.6

Your build automation software is extracted in apache-maven-3.8.6 directory.

Create a soft link as follows for easy accessibility and management.

# ln -s /opt/apache-maven-3.8.6 /opt/maven

Set Linux environment variables as required by Maven software.

For this purpose, create a file in /etc/profile.d directory by using vim text editor.

# vi /etc/profile.d/maven

Add following commands in this 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}

Grant executions permissions to /etc/profile.d/maven file.

# chmod +x /etc/profile.d/maven

Execute /etc/profile.d/maven script to setup Maven environment for your current Linux shell.

# source /etc/profile.d/maven

Verify the version of Apache Maven software.

# mvn --version
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/maven
Java version: 17.0.5, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-17-openjdk-17.0.5.0.8-2.el9_0.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.14.0-162.6.1.el9_1.0.1.x86_64", arch: "amd64", family: "unix"

To start using Maven in true pace, we recommend that you should buy and read following book.

 

Conclusion:

In this configuration guide, you have learned how to install Apache Maven on Rocky Linux 9 and other Red Hat based Linux distros.

If you find this article useful? Consider supporting us by Buy Me A Coffee


No comments:

Post a Comment

© 2022 CentLinux. All Rights Reserved.