How to Install Apache Maven on Amazon Linux 2

In this article we are going to cover Install java on Amazon Linux 2, Download maven, How to Install Apache Maven on Amazon Linux 2, setup Apache maven variables and check maven version on Amazon Linux 2.

Introduction

Maven is build automation tools basically used for the java project but maven also can be used for build and manage projects written in c# ,ruby, Scala and other language .Maven project hosted by Apache software foundation

Prerequisite

  • Amazon Linux 2 Instance
  • SSH access with sudo privileges

#1. Install Java on Amazon Linux 2

update the system packages

sudo yum update

Install java 1.8 on Amazon Linux 2 using below command

sudo yum install java-1.8.0-openjdk

To install java 11 on Amazon Linux 2 using below command

sudo amazon-linux-extras install java-openjdk11

Check the Installed OpenJDK version using command line

java --version

Output:

openjdk 11.0.9 2020-10-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.9+11-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.9+11-LTS, mixed mode, sharing)

use below command to switch java version

sudo alternatives --config java

How to Install Apache Maven on Amazon Linux 2

#2. Download Latest Apache Maven on Amazon Linux 2

Download the latest Apache Maven version on Ubuntu from Apache Maven official download page.

Navigate to /tmp directory

cd /tmp

Download the Apache Maven version using wget

sudo wget https://www-eu.apache.org/dist/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz

Output:

https://www-eu.apache.org/dist/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
Resolving www-eu.apache.org (www-eu.apache.org)... 95.216.26.30, 2a01:4f9:2a:1a61::2
Connecting to www-eu.apache.org (www-eu.apache.org)|95.216.26.30|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://downloads.apache.org/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz [following]
--2021-04-29 13:19:14--  https://downloads.apache.org/maven/maven-3/3.8.1/binaries/apache-maven-3.8.1-bin.tar.gz
Resolving downloads.apache.org (downloads.apache.org)... 88.99.95.219, 2a01:4f8:10a:201a::2
Connecting to downloads.apache.org (downloads.apache.org)|88.99.95.219|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9536838 (9.1M) [application/x-gzip]
Saving to: ‘apache-maven-3.8.1-bin.tar.gz’

100%[============================================================================>] 9,536,838   6.96MB/s   in 1.3s

2021-04-29 13:19:16 (6.96 MB/s) - ‘apache-maven-3.8.1-bin.tar.gz’ saved [9536838/9536838]

once downloaded extract the apache maven setup in /opt directory

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

Create a symlink for Apache maven directory to update the maven versions

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

#3. Setup Apache Maven Environment Variables

To setup apache maven environment variables in Amazon Linux 2, create a file named maven.sh in /etc/profile.d/ directory

sudo nano /etc/profile.d/maven.sh

Paste the below lines

export JAVA_HOME=/usr/lib/jvm/jre-11-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

Give the executable permission using chmod

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

Load the Environment Variables using source command

source /etc/profile.d/maven.sh

we have covered How to Install Apache Maven on Amazon Linux 2.

Verify the installed maven version using command line

mvn -version

Output:

Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /opt/maven
Java version: 11.0.9, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.9.11-0.amzn2.0.1.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.14.231-173.360.amzn2.x86_64", arch: "amd64", family: "unix"

So now we will remove the Apache Maven downloaded archive file because it is of no use now.

lets free up space by using below command which will remove the downloaded archive file.

sudo rm -f apache-maven-3.8.1-bin.tar.gz

Conclusion:

We have covered install java on Amazon Linux 2, Download maven, How to Install Apache Maven on Amazon Linux 2, setup Apache maven variables and check maven version on Amazon Linux 2.

Related Articles:

About Govind Hivrale

I am Govind Hivrale pursuing B.Tech Final Year, Likes to explore open source Tools and share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link