How to install Apache Maven on Rocky Linux 9

Share on Social Media

Unlock the power of dependency management and project build automation by learning how to install Apache Maven on Rocky Linux 9 with our comprehensive guide. Dive into the world of Java development with ease and efficiency with this step-by-step tutorial tailored for seamless integration on your Linux environment. #centlinux #linux #apachemaven

YouTube player

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.

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

What is Apache Maven used for?

Apache Maven is a powerful build automation and project management tool primarily used for Java projects, though it can be used for projects in other languages as well. Here are some common use cases for Apache Maven:

  1. Dependency Management: Maven simplifies the process of managing dependencies for your project by automatically downloading and including required libraries and dependencies from remote repositories. This ensures that your project has all the necessary dependencies to compile, test, and run successfully.
  2. Build Automation: Maven automates the process of building and packaging your project, including compiling source code, running tests, and packaging artifacts such as JAR (Java Archive) files, WAR (Web Archive) files, and others. Maven uses a standardized project structure and lifecycle phases to execute these tasks efficiently.
  3. Project Management: Maven provides a structured approach to managing projects, including defining project metadata, organizing source code, and specifying project dependencies and configurations in a standardized way. This makes it easier for developers to collaborate on projects and maintain consistency across different projects.
  4. Integration with IDEs: Maven integrates seamlessly with popular Integrated Development Environments (IDEs) such as Eclipse, IntelliJ IDEA, and NetBeans. IDEs can leverage Maven’s project structure and dependency management capabilities to provide features such as code completion, refactoring, and debugging.
  5. Plugin Ecosystem: Maven has a rich ecosystem of plugins that extend its functionality and allow developers to customize and automate various aspects of the build process. Plugins are available for tasks such as generating documentation, running code analysis tools, deploying artifacts to repositories, and more.
  6. Continuous Integration: Maven is often used in conjunction with Continuous Integration (CI) tools such as Jenkins, Travis CI, and CircleCI to automate the process of building, testing, and deploying software in a continuous integration and delivery pipeline. Maven’s standardized project structure and build lifecycle make it well-suited for integration with CI/CD workflows.

Overall, Apache Maven is a versatile tool that simplifies and automates various aspects of the software development process, from managing dependencies to building, testing, and deploying projects. Its standardized approach, rich feature set, and vibrant ecosystem make it a popular choice for Java developers and teams working on software projects of all sizes and complexities.

Apache Ant vs Maven

Apache Ant and Apache Maven are both popular build automation tools used primarily for Java projects, but they have some key differences in their approach and functionality:

  • XML vs. Convention over Configuration:
  • Apache Ant: Ant uses XML-based build scripts where developers explicitly define each step of the build process. Ant provides fine-grained control over the build process, allowing developers to customize it extensively.
  • Apache Maven: Maven follows a convention-over-configuration approach, where project configuration is based on a standardized project structure and predefined build lifecycle phases. Developers only need to specify deviations from the standard conventions, reducing the amount of configuration required.
  • Dependency Management:
  • Apache Ant: Ant does not have built-in support for dependency management. Developers need to manage dependencies manually, either by copying JAR files into the project or using a separate dependency management tool.
  • Apache Maven: Maven has robust built-in support for dependency management. It automatically downloads dependencies from remote repositories based on the project’s configuration, simplifying the process of managing project dependencies.
  • Plugin Ecosystem:
  • Apache Ant: Ant provides a flexible plugin mechanism, allowing developers to extend its functionality by writing custom tasks in Java or other languages. However, Ant’s plugin ecosystem is not as extensive as Maven’s.
  • Apache Maven: Maven has a rich ecosystem of plugins that extend its functionality and provide support for various tasks such as compiling code, running tests, generating documentation, and more. Maven plugins follow a standardized format and can be easily integrated into the build process.
  • Build Lifecycle:
  • Apache Ant: Ant allows developers to define custom build targets and execute them in any order. There is no predefined build lifecycle, so developers have full control over the sequence of build steps.
  • Apache Maven: Maven defines a standardized build lifecycle with predefined phases such as compile, test, package, install, and deploy. Developers can bind plugin goals to lifecycle phases, making it easier to understand and maintain the build process.
  • Learning Curve:
  • Apache Ant: Ant’s XML-based build scripts can be more verbose and require a deeper understanding of the build process. Developers have more control but may need to invest more time in writing and maintaining build scripts.
  • Apache Maven: Maven’s convention-over-configuration approach and standardized project structure make it easier for developers to get started with build automation. Maven’s predefined build lifecycle and dependency management simplify the build process, reducing the learning curve for new users.

In summary, Apache Ant provides fine-grained control and flexibility in build automation, while Apache Maven offers a more standardized and convention-based approach with built-in support for dependency management and a rich ecosystem of plugins. The choice between Ant and Maven depends on factors such as project requirements, developer preferences, and familiarity with each tool.

Recommended Online Training: Learning Apache Maven

475022 0312 4show?id=oLRJ54lcVEg&offerid=1074530.475022&bids=1074530

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 Rocky Linux Server

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

Install Apache Maven from Yum Repository

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

Therefore, you can easily install Apache Maven on Rocky Linux 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"

Final Thoughts

Congratulations on successfully learning how to install Apache Maven on Rocky Linux 9! With Maven at your disposal, you’re equipped with a powerful tool for managing dependencies and automating project builds in your Java development workflow. Dive into your projects with confidence, knowing you have the support of Maven to streamline your development process and propel your software endeavors forward. Happy coding!

Leave a Comment