How to install Apache Maven on Ubuntu 22.04

Estimated reading: 3 minutes 503 views

Apache Maven is a software project management/automation tool used primarily to manage Java project builds, reporting, and documentation from one central piece of information. This guide will help you install Apache Maven on Ubuntu 22.04

Maven exists to help you simplify and standardize all of your project build processes. Maven will handle authoring, distribution, documentation, team collaboration, and other project tasks seamlessly. It also increases reusability and takes care of most of the tasks involved.

Maven data structure:

Item Default
Source code ${basedir}/src/main/java
Resources ${basedir}/src/main/resource
Check ${basedir}/src/test
Bytecode compliance ${basedir}/target
Distributable JAR ${basedir}/target/class

Update the system and install Java

Apache Maven requires the server system to have the Java Development Kit. Install OpenJDK on Ubuntu/Debian.

apt update
apt install -y default-jdk tree

Check your installed Java version using:

$ java -version

Download and install Apache Maven

Check out the latest release of Apache Maven and save it to a variable:

export VER="3.9.6"

Download the latest release of Apache Maven exported above:

wget https://downloads.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz 

Now extract the downloaded tarball:

tar xvf apache-maven-3.9.6-bin.tar.gz

Move the resulting folder to /opt

mv apache-maven-3.9.6 /opt/maven

Set up environment variables to load Apache Maven

cat <<EOF | tee /etc/profile.d/maven.sh
export MAVEN_HOME=/opt/maven
export PATH=\$PATH:\$MAVEN_HOME/bin
EOF

File source

$ source /etc/profile.d/maven.sh
$ echo $MAVEN_HOME
/opt/maven
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/opt/maven/bin

You  $PATH should be  /opt/maven/bin at the end.

Verify installation

Finally, validate that Apache Maven has been installed correctly:

$ mvn --version

This command will print the Maven version and Java version used.

All options are available on:

mvn -h

Create a Maven project directory:

mkdir ~/mvn-projects && cd ~/mvn-projects

Create your first project:

mvn archetype:generate -DgroupId=com.mycompany.app \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false

Since this is the first run, executing may take some time. Maven downloads the most recent artifacts (plugin jars and other files) to your local repository.

When run successfully, you will get results similar to those below:

Created  generate goal a folder with the same name as ArtifactId.

$ tree my-app/

  • The directory  src/main/javacontains the project source code
  • The directory  src/test/java contains the test source
  • This file  pom.xml is the project’s Project Object Model (POM).

This file pom.xml is the core of the project configuration in Maven. It contains most of the information needed to build a project.

Project construction

To build your Project, run:

root@Greencloud:~/mvn-projects# ls
my-app
root@Greencloud:~/mvn-projects# cd my-app/
root@Greencloud:~/mvn-projects/my-app# ls
pom.xml src
root@Greencloud:~/mvn-projects/my-app# mvn package

The command line will print out various actions and end with the following information:

So you have successfully installed Apache Maven on Ubuntu 22.04. Check out the official Maven Documentation to learn more.