Apache Maven is a tool for building automation and software, mainly used for Java projects. It uses a project object model (POM) to provide a consistent build system with consistent use in all projects. Maven also delivers dependency management, extensibility through plugins and integration with Git to manage issues. It can also generate information about your project (an example of this 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.
- A Debian 10 instance where you have access to the root user or a user with sudo privileges.
If you are using a sudo user, start and use a root shell for the duration of this installation:
: Install a JDK
Update your package index and install any updates using commands:
apt update apt upgrade -y
Then install the packages default-jdk wget and git . The first one provides a Java-compatible development package (OpenJDK 11), the wget will be used to download the Maven, and git is required for the test step.
apt install -y default-jdk wget git
Maven requires that $ JAVA_HOME environment variable be set. You can set it all up with the following command:
echo "export JAVA_HOME = / lib / jvm / default-java" >> / etc / profile
Step 2: Install Apache Maven
First Download and Import the public keys used by 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 format "Binary tar.gz" 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
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 with the following command. The signature should match one of the keys previously imported.
gpg - verify maven.tgz.asc maven.tgz
Make sure the resulting output contains Good signature before proceeding. For example:
gpg: Signature made Tue 27 Aug 2019 05:10:12 PM CEST gpg: with RSA key BBE7232D7991050B54C8EA0ADC08637CA615D22C gpg: Good signature from "Enrico Olivelli <[email protected]>" [unknown]
Then extract and move the resulting directory with:
tar -xzf maven.tgz rm maven.tgz * etc. apache-belly * / opt / belly
Then add / opt / maven / bin 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 your stomach. Try the following:
You should see an output similar to the following:
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-27T17: 06: 16 + 02: 00) Maven hem: / opt / maven Java version: 11.0.5, vendor: Debian, runtime: / usr / lib / jvm / java-11-openjdk-amd64 Default setting: 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 to put together a simple Hello World project with Maven to verify that it works properly. Clone this Github archive with the following commands:
cd / tmp gitklon https://github.com/nxnjz/java_maven_helloworld.git
Build the project with Maven:
cd java_maven_helloworld / mvn package
mvn package runs each build phase to up to the package phase. By default, this includes the following main phases: validate, compile, test, package. After success, it will end with the following output:
[INFO] --------------------------------- - -------------------------------------- [INFO] BUILD SUCCESS [INFO] ----------------------------------------------- -------------------------
We can now run the compiled JAR:
java -cp target / hello-world-0.1.0.jar hello.HelloWorld
If you encounter problems following this guide, please leave a comment below.