How to Install Apache Maven on Ubuntu System: A Step-by-Step Guide

Step-by-step guide to install Apache Maven on Ubuntu system for software development.

How to Install Apache Maven on Ubuntu System: A Step-by-Step Guide

What is Apache Maven?

“Apache Maven is a tool for software management and automation that aids developers in managing dependencies and automating the build process for Java projects. It helps users compile, test, and package applications efficiently and can be integrated with CI/CD tools such as Jenkins and GitLab for automated workflows.”

How to Install Apache Maven on Ubuntu System

Before starting the installation, make sure your system meets the following prerequisites.

Prerequisites for Installing Apache Maven on Ubuntu System

A Linux System (Ubuntu Preferred)

Maven works with various Linux distributions, but we recommend using Ubuntu in this guide because of its built-in package manager support, which simplifies the installation process.

Basic Command Line Knowledge

You should have a basic understanding of the Linux command line to install and manage Maven.

Administrative Privileges

Ensure you have sudo access to install software and configure environment variables on your system.

JDK 17 or Higher

Apache Maven requires a Java Development Kit (JDK) to work properly. It is recommended to use JDK 17 for better compatibility and performance.

Installation Methods for Apache Maven

Installing Apache Maven on Ubuntu System via APT

The easiest method to install Maven is by using the APT package manager. However, this method may not provide the most recent version of Maven.

Steps:

  • Update the system repository index with the following command: sudo apt update
  • Install Maven with the command: sudo apt install maven

Verifying the Installation: After the installation, verify that Maven was installed properly by checking its version: mvn -version

Installing Apache Maven on Ubuntu System via Binary Distribution

If you need more control over Maven’s version and configuration, you can install it using the official binary distribution.

Steps:

  • Download the Maven binary archive using wget : wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
  • Extract the downloaded file: tar -xvf apache-maven-3.9.9-bin.tar.gz
  • Move the extracted Maven folder to a directory of your choice (e.g., /opt/): sudo mv apache-maven-3.9.9 /opt/

Verifying the Checksum: To make sure the downloaded file is correct, verify the checksum:

  • Download the SHA512 checksum file: wget https://downloads.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz.sha512
  • Run the checksum verification command: sha512sum -c apache-maven-3.9.9-bin.tar.gz.sha512

Setting Up Environment Variables

To make Maven accessible across your system, set the environment variables:

  • Add the following lines to your .bashrc or .profile: export M2_HOME=/opt/apache-maven-3.9.9 export PATH=$M2_HOME/bin:$PATH

Verifying the Installation: Run the following command to check that Maven was installed correctly: mvn -version

Installing Apache Maven on Ubuntu System via SDKMAN

SDKMAN is a great tool for managing multiple Maven versions, as well as other Java-related software. It simplifies switching versions.

Steps:

  • Install SDKMAN: curl -s "https://get.sdkman.io" | bash
  • Load SDKMAN into the current session: source "$HOME/.sdkman/bin/sdkman-init.sh"
  • Install Maven 3.9.9: sdk install maven 3.9.9

Verifying the Installation: After installation, verify that Maven is installed by running: mvn -version

Installing Apache Maven on Ubuntu System via Automation Scripts

For CI/CD environments, using automation scripts can help achieve reproducible, consistent installations.

Steps:

  • Create an automation script (e.g., using Ansible, Bash, or Terraform) to install Maven on all your machines or containers.
  • Use a script that automates the installation steps and configurations to ensure a consistent environment across all instances.

Configuring Apache Maven After Installation

Setting JAVA_HOME and M2_HOME

After installing Maven, set the required environment variables to ensure both Maven and Java are recognized correctly by your system.

Configuration Example for APT-based OpenJDK 17:

  • Set JAVA_HOME to point to the JDK installation: export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
  • Set M2_HOME to point to the Maven installation directory: export M2_HOME=/opt/apache-maven-3.9.9
  • Update the PATH variable: export PATH=$JAVA_HOME/bin:$M2_HOME/bin:$PATH

Configuring System-Wide Environment Variables

To apply the changes globally, create a new script file in /etc/profile.d/:

Create the script file: sudo tee /etc/profile.d/maven.sh > /dev/null << 'EOF' export M2_HOME=/opt/apache-maven-3.9.9 export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH="$JAVA_HOME/bin:$M2_HOME/bin:$PATH" EOF

Make the script executable: sudo chmod +x /etc/profile.d/maven.sh

Verifying Maven and Java Installation: To verify that Maven and Java are correctly installed and configured, run the following commands: mvn -version java -version

Sample Maven Project for Testing

Creating a simple Maven project will allow you to verify that Maven is functioning properly.

Steps:

  • Create a directory for your project: mkdir maven-hello-world cd maven-hello-world
  • Generate a sample project using the Maven archetype: mvn archetype:generate -DgroupId=com.example -DartifactId=hello-world -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false cd hello-world

Compiling the Project

To compile the generated project, run the following command: mvn compile

Packaging the Project

To package the project into a JAR file, use: mvn package

Running the Application

Run the application with the following command: java -cp target/hello-world-1.0-SNAPSHOT.jar com.example.App This should display the message "Hello World!" if Maven is working correctly.

Advanced Maven Usage in CI/CD Pipelines

Integrating Maven with Docker for CI/CD

Maven can be integrated into Docker containers to automate builds in isolated environments. The following Dockerfile installs Maven and JDK 17 in an Ubuntu-based container.

Example Dockerfile:

 FROM ubuntu:22.04
 RUN apt-get update && apt-get install -y openjdk-17-jdk maven
 WORKDIR /app
 COPY . .
 RUN mvn clean install

This setup ensures that the Maven build process operates in a controlled and reproducible environment, making it ideal for CI/CD pipelines.

Performance Optimization in Maven Builds

For larger projects, Maven builds can take a significant amount of time. To improve build performance, Maven allows parallel execution of tasks.

Enabling Parallel Execution: Use the -T option to define the number of threads to use for parallel builds: mvn -T 1C clean install This command will use one thread per core available on your machine, speeding up the build process.

Jenkins Integration

To integrate Maven into Jenkins pipelines, create a Jenkinsfile that defines the build process.

Example Jenkinsfile:

 pipeline {
   agent any
   stages {
     stage('Build') {
       steps {
         script {
           sh 'mvn clean install'
         }
       }
     }
   }
 }

GitLab CI/CD Configuration

Integrating Maven into GitLab CI/CD pipelines is possible by using a .gitlab-ci.yml file that runs Maven commands.

Example .gitlab-ci.yml:

 stages:
   - build
 build:
   script:
     - mvn clean install

GitHub Actions Integration

For GitHub Actions, Maven can be integrated into the workflow by defining steps in the yaml configuration.

Example GitHub Actions Configuration:

 name: Maven Build
 on:
   push:
     branches:
       - main
 jobs:
   build:
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v2
       - uses: actions/setup-java@v2
         with:
           java-version: '17'
       - run: mvn clean install

Troubleshooting Apache Maven Installation

Common Installation Issues

Maven Commands Not Found

Ensure both JAVA_HOME and M2_HOME environment variables are set correctly. Also, make sure Maven's bin directory is in the PATH.

Switching Java Versions

To switch between Java versions on Ubuntu, use the update-alternatives command: sudo update-alternatives --config java This lets you choose the default Java version for your system.

Maven Build Failures

If the Maven build fails with "Could not find artifact" errors, check your internet connection and verify the repository settings in ~/.m2/settings.xml . You may also need to clear the local repository cache.

Uninstalling Apache Maven

Uninstalling Maven via APT

To uninstall Maven installed via APT, use the following command: sudo apt remove maven

Uninstalling Maven via Binary Distribution

If Maven was installed using the binary distribution, remove it by deleting the Maven directory: sudo rm -rf /opt/apache-maven-3.9.9

Removing Environment Variables

To remove the environment variables, delete the corresponding entries from .bashrc or .profile.

Official Maven Documentation

Any Cloud Solution, Anywhere!

From small business to enterprise, we’ve got you covered!

Caasify
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.