Install Apache Maven on Ubuntu 18.04

Table of Contents

Install Apache Maven on Ubuntu 18.04

 

 

Java projects usually use Maven as their build automation tool. In addition to C#, Ruby, and Scala, Maven can also be used to build and manage projects. It was formerly part of the Jakarta Project, which is now hosted by the Apache Software Foundation.

My goal in this tutorial is to walk you through step-by-step the installation and configuration of Apache Maven on Ubuntu 18.04.

Prerequisites 

  • A newly deployed VPSie Ubuntu 18.04 server.

 

  • A non-root user with sudo privileges created on your server.

 

Let’s get started,

 

Step 1: Update your server

First, Install the latest version of your operating system:

 

 

sudo apt-get update -y

 

sudo apt-get upgrade -y

 

Step 2: Install Java

 

JDK 1.7 or higher is required for installing Maven 3.3 or higher. During this installation, OpenJDK will be installed, which is the default Java runtime and development environment in Ubuntu 18.04. 

 

Install OpenJDK: 

 

sudo apt-get install -y default-jdk

 

You can verify the Java version by : 

java -version

 
As a result, the output will be as follows: 

openjdk version "16.0.2" 2021-08-20
OpenJDK Runtime Environment (build 16.0.2+13-Ubuntu-1ubuntu0.18.04.3)
OpenJDK 64-Bit Server VM (build 16.0.2+13-Ubuntu-1ubuntu0.18.04.3, mixed mode)

 

 

Step 3: Install Apache Maven

 

First, change your working directory to the /opt/ directory:

cd /opt/

You can visit the official website to download Apache Maven’s latest stable release:

 

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

 

Once you have downloaded the archive, extract it:

 

sudo tar -xvzf apache-maven-3.6.0-bin.tar.gz

 

The next step is to rename the extracted directory:

 

sudo mv apache-maven-3.6.0 maven 

Step 4: Setup environment variables

Afterward, you will need to set up your environment variables, such as M2_HOME, JAVA_HOME and PATH. You can do this by creating a mavenenv.sh file inside of the /etc/profile.d/ directory:

sudo vi /etc/profile.d/mavenenv.sh

Add the following lines:

 

export JAVA_HOME=/usr/lib/jvm/default-java
export M2_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}

 

Save and close the file, and make it executable:

 

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

Now you can load the environment variables:

source /etc/profile.d/mavenenv.sh

Step 5: Verify installation

After all the configurations have been successful, check the Apache Maven version:

mvn --version

You will see something similar to the following:

 

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2021-08-24T18:41:47Z)
Maven home: /opt/maven
Java version: 16.0.2, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-36-generic", arch: "amd64", family: "unix"

It is a pleasure for me to congratulate you on the successful installation of Apache Maven on your Ubuntu 18.04 server.

 

That’s it!

The purpose of this article is to show you how to install Apache maven on Ubuntu 18.04, and we hope you now have a better understanding of how this process works.

 

 

BPYTOP on Ubuntu

Apache Maven is a software project management and comprehension tool. It manages project builds, dependencies, and documentation using a central piece of information called a Project Object Model (POM).

Apache Maven provides a standard way to manage dependencies and builds for Java projects. It simplifies the process of building, testing, and deploying Java applications.

You can create a new Maven project using the following command: mvn archetype: generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This will create a new Maven project with the group ID “com.mycompany.app” and artifact ID “my-app.”

You can build a Maven project by navigating to the project directory and running the following command: mvn clean install
This command will compile the source code, run tests, and create a project JAR file.

 You can add a dependency to a Maven project by adding the dependency information to the project’s POM file. For example, to add the JUnit dependency, add the following code to the POM file:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>

You can deploy a Maven project to a remote repository by adding the repository information to the project’s POM file and running the following command: mvn deploy

You can run a Maven project using the following command: mvn exec: java -Dexec.mainClass=”com.mycompany.app.App”
This command will execute the “main” method of the “App” class in the “com.mycompany.app” package.

To configure a proxy for Maven, add the following code to the “settings.xml” file:
<proxies> <proxy> <id>my-proxy</id> <active>true</active> <protocol>http</protocol> <host>proxy.example.com</host> <port>8080</port> <username>proxyuser</username> <password>proxypass</password> </proxy> </proxies>
Replace “my-proxy,” “proxy.example.com,” “8080”, “proxy user”, and “proxypass” with the appropriate values for your proxy configuration.

You can customize the Maven build process by adding plugins and goals to the project’s POM file. Plugins are used to perform specific tasks during the build process, such as compiling code, running tests, and generating documentation. Goals are specific tasks that can be executed within a plugin. You can also create your plugins and goals to extend the functionality of Maven.

Make a Comment
Share on
Facebook
Twitter
LinkedIn
Print
VPSie Cloud service

Fast and Secure Cloud VPS Service

Try FREE
For a month

The First 1 orders gets free discount today! Try Sign up on VPSie to get a chance to get the discount.