Installation of Maven in Linux

Installation of Maven in Linux

"Step-by-Step Guide to Installing Maven on Linux"

Here's a step-by-step guide to installing Maven on a Linux system:

1. Update the System

First, update your system’s package index to ensure you have the latest versions of software and dependencies:

sudo apt update

2. Install Java Development Kit (JDK)

Since Maven is a Java-based application, installing Java is a prerequisite.You can install OpenJDK using the following command:

sudo apt install openjdk-11-jdk

(You can replace openjdk-11-jdk with any supported version, such as openjdk-8-jdk or openjdk-17-jdk.)

Since I am installing this on the Amazon Linux server I will be using the below
[ec2-user]# amazon-linux-extras install java-openjdk11 -y

after installing Java check your Java version using the below command java —version

Now let’s install git using yum install git -y
now let’s create one directory (this will be our local repository) Because Maven installs the artifacts in the local repository

In Maven, an artifact refers to a file that is produced by a build process, typically a JAR, WAR, or EAR file, but it can be any type of file that Maven manages during the build lifecycle. Artifacts are the primary output of a project, and they are used for further development, distribution, or deployment.

Key points about Maven artifacts:

  1. Artifact ID: This is a unique identifier for the artifact, usually associated with the project’s name.

  2. Version: The version number of the artifact, indicating its release.

  3. Group ID: A group identifier used to uniquely group artifacts in a repository, typically following the reverse domain name notation (e.g., com.company).

  4. Packaging: The type of artifact, like JAR, WAR, or EAR.

  5. Repository: Artifacts are stored in repositories (local or remote), where they can be downloaded or shared.
    In summary, an artifact in Maven is a file that contains the output of the build process, such as compiled code (JAR/WAR), dependencies, or other project-related resources. Maven uses these artifacts for managing dependencies and facilitating builds.

Let's create a local repository and name it "projects" using the command mkdir projects. Initially, this repository will not contain any artifacts. If you're working for a company, they typically provide the necessary artifacts. However, for practice, let's download some standalone artifacts.

Now go to https://github.com/MithunTechnologiesDevOps/maven-standalone-application
Here go to Code section → https → copy the https URL as shown below

Now come back to your server
Now I will git clone to clone the repository
Difference Between git clone and git fork

The distinction between git clone and git fork lies in their functionality and how they are used, particularly in collaborative environments like GitHub or other Git hosting platforms.

1. git clone

  • Definition: git clone is a Git command that creates a local copy of an existing repository on your machine.

  • Usage: You use git clone when you want to download the full repository (including its entire history) from a remote server (like GitHub) to your local system.

  • Action: This command creates a local copy of the repository, which is linked to the original remote repository, allowing you to work with the code locally.


2. git fork

  • Definition: git fork is an action that occurs on Git hosting platforms like GitHub, GitLab, or Bitbucket, creating a personal copy of a repository under your own account.

  • Usage: You use git fork when you want to contribute to a project where you do not have direct write access. Forking enables you to make changes and submit them through pull requests (PR).

  • Action: A fork duplicates the entire repository into your GitHub account, allowing you to modify it. Later, you can push changes to your forked repository and open a pull request to suggest updates to the original repository.


In summary:

  • git clone is used to copy a repository to your local machine, keeping it connected to the original source.

  • git fork is used to create a personal copy of the repository on a Git hosting service, allowing you to propose changes back to the original repository.

Now lets clone the repo
use the command git clone https://github.com/MithunTechnologiesDevOps/maven-standalone-application.git

now when you ls you will see the maven’s stand-alone application is cloned to your local repo

Now lets install Maven software and we will later get back to this repository
Remember you always install the software in the opt directory
navigate to the opt directory refer to the screenshot

Go to https://maven.apache.org/download.cgi
Copy the link address of the tar.gz file

Use the command wget < URL of the copied address>and enter

now extract this tar.gz using tar xf apache-maven-3.9.9-bin.tar.gz
explanation of tar xf apache-maven-3.9.9-bin.tar.gz
The command
tar xf apache-maven-3.9.9-bin.tar.gz is used to extract the contents of a compressed .tar.gz archive (in this case, the Apache Maven binary archive). Let's break down the command:

1. tar:

  • tar stands for "tape archive", a utility used for compressing and extracting files from archive files.

  • It is commonly used in Unix-like systems (Linux, macOS) to create and manipulate archive files.

2. x (Extract):

  • The x option stands for "extract". It tells tar to extract files from an archive.

  • This option is used when you want to unpack or decompress a .tar, .tar.gz, or .tgz file.

3. f (File):

  • The f option specifies that the next argument will be the name of the archive file to operate on.

  • In this case, it indicates that the file apache-maven-3.9.9-bin.tar.gz is the archive you want to extract.

4. apache-maven-3.9.9-bin.tar.gz:

  • This is the name of the .tar.gz file you want to extract.

  • It’s the binary distribution of Apache Maven version 3.9.9, which is typically downloaded from the official Maven website.

  • The .tar.gz extension indicates it's a compressed archive file (a .tar file that has been compressed using gzip).

  • Now I have to set the environment variable meaning I have to tell the server where I have installed the Java and where I have installed the maven

  • To set the environment variable
    Go to Vi editor using sudo vi ~/.bash-profile
    vi editor will open click on I to insert the below code

  •   #java-11-openjdk-11.0.22.0.7-1.amzn2.0.1.x86_64
      export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.23.0.9-2.amzn2.0.1.x86_64"
      export MAVEN_HOME=/opt/apache-maven-3.9.9
      export M2=/opt/apache-maven-3.9.9/bin
      PATH=$PATH:$HOME/bin:$JAVA_HOME/bin:$MAVEN_HOME:$M2
      #openjdk 11.0.23 2024-04-16 LTS
      #OpenJDK Runtime Environment (Red_Hat-11.0.23.0.9-2.amzn2.0.1) (build 11.0.23+9-LTS)
      #OpenJDK 64-Bit Server VM (Red_Hat-11.0.23.0.9-2.amzn2.0.1) (build 11.0.23+9-LTS, mixed mode, sharing)
      #openjdk 11.0.23 2024-04-16 LTS
    

Once done run the file source ~/.bash_profile
and now check the Maven version using -bash-4.2# mvn --version

This ends the session on how you install Maven on Linux

\=============================================================
Don’t forget to hit the follow button on the top right of our homepage
Support Awstronaut ☁️🚀 - buymeacoffee.com/awstronaut ☕💻