In this article we are going to cover How to Install Maven on Ubuntu 20.04 LTS , Downloading latest version of maven, configuring environment variables for maven and check maven version using command line.
Table of Contents
Introduction
Apache Maven is a tool for building and managing Java-based projects.
In java based project you have to manage a pom.xml file. It contains information about the project and configuration used by Maven to build the java based project. Everything for your Maven build is defined within pom.xml file.
Why we used Maven?
Maven made so easy to compile, test, package an deploy our app. We only needed one command to get or application deployed.
What are the Maven Goals ?
- Making the build process
- Providing project information
What is Maven Build Lifecycle ?
Below are phases of Maven Build Lifecycle
- Validate
- Compile
- Test
- Package:
- Integration-test
- Verify:
- Install:
- Deploy:
- Clean:
- Site
Prerequisites
- Ubuntu 20.04 LTS with minimal Installation
- SSH Access with sudo privileges
How to Install Maven on Ubuntu 20.04 LTS using APT
Update the system packages
sudo apt update
Install maven on Ubuntu 20.04 LTS using APT
sudo apt install maven
Check Maven Version using command line on Ubuntu 20.04 LTS
mvn -version
Output:
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.10, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-1038-aws", arch: "amd64", family: "unix"
To remove maven from Ubuntu 20.04 LTS using APT
sudo apt remove maven
How to Install Maven on Ubuntu 20.04 LTS by downloading Apache Maven Binaries
update the system packages
sudo apt update
Install OpenJDK 11 on Ubuntu 20.04 LTS
sudo apt install default-jdk -y
Check the Install OpenJDK version using command line
java --version
Output:
openjdk 11.0.10 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.10+9-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
Downloading Latest Apache Maven on Ubuntu 20.04 LTS
Download the latest Apache Maven version on Ubuntu from Apache Maven official download page. Here we are downloading Apache Maven version 3.6.3.
Navigate to /tmp directory
cd /tmp
Download the Apache Maven version using wget
wget http://www-eu.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
Output:
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|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz [following]
--2021-03-19 14:24:19-- https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-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: 9506321 (9.1M) [application/x-gzip]
Saving to: ‘apache-maven-3.6.3-bin.tar.gz’
apache-maven-3.6.3-bin.tar.gz 100%[====================================================================================================>] 9.07M 5.39MB/s in 1.7s
2021-03-19 14:24:21 (5.39 MB/s) - ‘apache-maven-3.6.3-bin.tar.gz’ saved [9506321/9506321]
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.6.3 /opt/maven
Setup Apache Maven Environment Variables
To setup apache maven environment variables in ubuntu 20.04 LTS, 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/default-java
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
Verify the installed maven version using command line
mvn -version
Output:
Maven home: /opt/maven
Java version: 11.0.10, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-1038-aws", arch: "amd64", family: "unix"
Conclusion:
We have covered How to Install Maven on Ubuntu 20.04 LTS , configuring environment variables for maven and check maven version using command line.
Related Articles