How to Install Apache Maven on Debian 10

Apache Maven is a build automation and software project management tool, mainly used for Java projects. It uses a project object model (POM) to provide a uniform build system with consistent usage across all projects. Maven also delivers dependency management, extensibility through plugins, and integration with Git for managing releases. It can generate information about your project as well (an example of this can be found here).

In this tutorial, we will be installing Apache Maven on a Debian 10 system and testing it with a simple java application.

Requirements

  • A Debian 10 instance on which you have access to the root user or a user with sudo privileges.

If using a sudo user, start and use a root shell for the length of this setup:

sudo -s

Step 1: Installing a JDK

Update your package index and install any updates with the commands:

apt update
apt upgrade -y

Then install the default-jdk, wget, and git packages. The first provides a java-compatible development kit (OpenJDK 11), wget will be used to download Maven, and git is required for the testing step.

apt install -y default-jdk wget git

Maven requires the $JAVA_HOME environment variable to be set. You can set it system-wide with the following command:

echo "export JAVA_HOME=/lib/jvm/default-java" >> /etc/profile

Step 2: Installing Apache Maven

First, download and import the public keys used by the Apache Maven developers. These will be used to verify

cd /tmp
wget https://www.apache.org/dist/maven/KEYS
gpg --import KEYS && rm KEYS

Then browse to the Apache Maven Download Page and copy the latest download links for the “Binary tar.gz archive” format and its corresponding signature file. Download them as follows:

wget -O maven.tgz LINK.tar.gz
wget -O maven.tgz.asc LINK.tar.gz.asc

For example:

wget -O maven.tgz https://www-eu.apache.org/dist/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz
wget -O maven.tgz.asc https://www.apache.org/dist/maven/maven-3/3.6.2/binaries/apache-maven-3.6.2-bin.tar.gz.asc

And verify the signature using the following command. The signature should match one of keys imported earlier.

gpg --verify maven.tgz.asc maven.tgz

Make sure the resulting output contains Good signature before proceeding. For instance:

gpg: Signature made Tue 27 Aug 2019 05:10:12 PM CEST
gpg:                using RSA key BBE7232D7991050B54C8EA0ADC08637CA615D22C
gpg: Good signature from "Enrico Olivelli <[email protected]>" [unknown]

Then unpack and move the resulting directory with:

tar -xzf maven.tgz
rm maven.tgz*
mv apache-maven* /opt/maven

Next, add the /opt/maven/bin directory to your $PATH environment variable:

echo "export PATH=$PATH:/opt/maven/bin" >> /etc/profile

Source /etc/profile to load the new environment variables with:

. /etc/profile

You should now be able to use maven. Try the following:

mvn -v

You should see an output similar to the following:

Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T17:06:16+02:00)
Maven home: /opt/maven
Java version: 11.0.5, vendor: Debian, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.19.0-6-amd64", arch: "amd64", family: "unix"

Step 3: Testing Maven

This step is optional.

We can try compiling a simple Hello World project with Maven to verify that it’s working properly. Clone this Github repository with the following commands:

cd /tmp
git clone https://github.com/nxnjz/java_maven_helloworld.git

Build the project with Maven:

cd java_maven_helloworld/
mvn package

The mvn package command runs each build phase in order up to the package phase. By default, this includes the following major phases: validate, compile, test, package. Upon success, it will exit with the following output:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

We can now execute the compiled JAR:

java -cp target/hello-world-0.1.0.jar hello.HelloWorld

If you encounter issues while following this guide, please feel free to leave a comment below.

Brought to you by Web Designer
Kuala Lumpur

Leave a Reply

Your email address will not be published. Required fields are marked *

*