How to install Apache Maven on Debian 10

How to install Apache Maven on Debian 10

Apache Maven is a build automation and software project management tool primarily for Java projects. It uses the Project Object Model (POM) to provide a unified build system and maintain consistent usage across all projects. Maven also provides dependency management, managing distributions through the extensibility of plugins and integration with Git. It can also generate information about your project (an example of which can be found here).

In this tutorial, we will install Apache Maven on a Debian 10 system and test it with a simple Java application.

Claim

  • You can access the Debian 10 instance of the root user or a user with sudo privileges.

If using a sudo user, start and use the root shell to make this setting:

sudo -s

Step 1: Install the JDK

Update the package index and install all updates using the following command:

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 test steps.

apt install -y default-jdk wget git

Maven needs to set the $ JAVA_HOME environment variable. You can set it system-wide using the following command:

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

Step 2: Install Apache Maven

First, download and import the public key used by Apache Maven developers. These will be used for verification

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 link 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

E.g:

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 use the following command to verify the signature. The signature should match one of the previously imported keys.

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

Before proceeding, ensure that the resulting output contains a good signature. E.g:

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 unzip and move the results directory using the following command:

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

Next, add the / opt / maven / bin directory to the $ 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. Please try the following:

mvn -v

You should see 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: Test Maven

This step is optional.

We can try to compile a simple Hello World project using Maven to verify that it works properly. Clone this Github repository using the following command:

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

Build the project using Maven:

cd java_maven_helloworld/
mvn package

The mvn package command runs each build phase in sequence until the packaging phase. By default, this includes the following main stages: verification, compilation, testing, packaging. Upon success, it will exit with the following output:

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

Now we can execute the compiled JAR:

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

More information

  • What is Maven
  • Maven Getting Started Guide

If you run into problems following this guide, feel free to leave a comment below.

Source

Sidebar