How to Install Apache Maven on Debian

Introduction

Write an introduction on How to Install Apache Maven on Debian

How to Install Apache Maven on Debian

How to Install Apache Maven on Debian

Introduction

Maven is an automation tool primarily used for Java projects. It simplifies project management by automatically downloading required libraries, managing their versions, and reducing manual effort and potential conflicts.

The following text explains how to install Maven on Debian using two different methods.

How to Install Maven on Debian

Prerequisites

  • A system running Debian (this tutorial uses Debian 12).
  • Access to a user account with sudo or root privileges.
  • Access to the terminal.
  • Java Development Kit (JDK) installed.

Note: Apache Maven requires the Java Development Kit (JDK) 8 or above. If you don’t have it, install it with sudo apt install default-jdk.

Install Maven on Debian via Package Manager

The easiest way to install Maven on Debian is via the package manager. To accomplish that, take the following steps:

1. Update the repository index:

sudo apt update
sudo apt update terminal outputsudo apt update terminal output

2. Install Apache Maven by entering the following:

sudo apt install maven
sudo apt install maven terminal outputsudo apt install maven terminal output

3. To verify Maven is installed, run the following command:

mvn --version
terminal output for mvn --versionterminal output for mvn --version

The system shows information about the Apache Maven version, the location of Maven Home, and the Java version currently installed.

Install Maven on Debian via the Official Archive

Installing Maven on Debian via the official archive requires more manual configuration and maintenance than using the package manager.

However, this type of installation allows for version flexibility, customization, and access to the latest software. To install Maven, take the following steps.

Step 1: Download the Official Maven Archive

Use the wget command to download the files to the /tmp directory. The /tmp directory is designed to store temporary files and data. Run the following:

sudo wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz -P /tmp
download official Maven archive on Debiandownload official Maven archive on Debian

At the time this article was written, Apache Maven was at version 3.9.6. To get the latest version, visit the developer’s download page and copy the link.

Step 2: Extract Maven Archive

Extract the .tar.gz file to the /opt directory. The /opt directory holds optional software installations, keeping the main filesystem clean. To extract files, run the tar command:

sudo tar xf /tmp/apache-maven-3.9.6-bin.tar.gz -C /opt

The command has no output, but -C ensures files are extracted in the /opt directory.

Note: If you’re using a different Maven version, change 3.9.6 to the version you downloaded.

After extracting files, create a symbolic link to the software location. This shortcut makes it easier to access the software without navigating directly to its directory. To accomplish this, run the following:

sudo ln -s /opt/apache-maven-3.9.6 /opt/maven

The command has no output.

Step 3: Configure Environment Variables

Like most Linux software, Apache Maven uses a configuration file. This file should be located at /etc/profile.d/maven.sh. If the file is not present, create it. Follow these steps:

1. Open or create the file in a text editor of choice. For example, to create a file with Nano, run:

sudo nano /etc/profile.d/maven.sh

2. Enter the following lines:

# Apache Maven Environmental Variables

# Set Java Home
export JAVA_HOME=/usr/lib/jvm/default-java

# Set Maven Home (choose either M2_HOME or MAVEN_HOME based on your preference)
export M2_HOME=/opt/maven
# export MAVEN_HOME=/opt/maven

# Add Maven bin directory to PATH
export PATH=${M2_HOME}/bin:${PATH}
Editing the Maven configuration file in Nano.Editing the Maven configuration file in Nano.

Save the file and exit.

3. Change the file permissions by entering the following:

sudo chmod +x /etc/profile.d/maven.sh

The command has no output but allows the file to be executed as a script or program.

4. Load the file with:

source /etc/profile.d/maven.sh

The command does not output anything. However, it ensures that changes made to environment variables or other settings within the file are applied immediately.

Step 4: Verify Maven Installation

Enter the following:

mvn –version
mvn --version terminal outputmvn --version terminal output

The system displays the Apache Maven version, the Maven Home location, and the Java version.

Creating a Project With Maven

Maven simplifies project setup by automating dependencies, building configurations, and project directory structures. This procedure makes it easier for developers to manage Java projects efficiently.

For instance, create a simple project by following the steps below.

Step 1: Create New Directory

Create a new directory for the Maven project. Name it maven-project1, for example. Use the mkdir command:

mkdir maven-project1

The command has no output. To navigate to the project directory, use the cd command:

cd maven-project1
cd terminal outputcd terminal output

Step 2: Create the pom.xml File

The pom.xml file in Maven is an essential configuration file that defines a Maven project’s structure, dependencies, plugins, and build settings. While in the project directory, create pom.xml with a text editor of choice. For instance, in Nano:

nano pom.xml

Add the following content to the file:


    4.0.0
    com.example
    maven-project1
    1.0-SNAPSHOT
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                
            
        
    
maven-project1 in pom.xml filemaven-project1 in pom.xml file

The configuration file consists of:

  • . The root element that defines the Maven project.
  • . Sets the Project Object Model (POM) version to which the file adheres. The POM model version defines the structure and elements used in the pom.xml file.
  • . Specifies the project group ID. In this case, it’s com.example.
  • . Sets the project artifact ID. It should match the project name, which is maven-project1 in this case.
  • . Specifies the project version. The value 1.0-SNAPSHOT is a common convention for the initial development version.
  • . Contains the configuration for the Maven build process.
  • . Defines Maven plugins to be used in the build process.
  • . Specifies a Maven plugin. In this case, the maven-compiler-plugin is used to compile Java code.
    • . Sets the plugin group ID.
    • . Determines the plugin artifact ID.
    • . Specifies the plugin version to use.
    • . Allows a user to configure plugin-specific settings. This example sets the Java source and target versions to 1.8.

After adding the text, save and exit the file.

Step 3: Create Java Source Code

To create Java source code, follow these steps:

1. Create a directory named src/main/java/com/example inside the project directory. Use mkdir with the -p option to create parent directories if they don’t exist:

mkdir -p src/main/java/com/example

The command has no output

2. Navigate to the src/main/java/com/example directory:

cd src/main/java/com/example
terminal output for cdterminal output for cd

3. Inside the com/example directory, create a Java file named App.java using a text editor. For example, with Nano:

nano App.java

4. Paste the following inside the App.java file:

package com.example;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, Maven!");
    }
}
App.java fileApp.java file

The file consists of:

  • package com.example;. The package declaration defines the package to which the Java file belongs. Packages help organize and manage code.
  • public class App { ... }. The class declaration that defines a class called App, which is a blueprint for objects in Java.
  • public static void main(String[] args) { ... }. The main method that represents the entry point of the Java program. Executes the code when the program starts.
    • String[] args. Allows passing command-line arguments.
  • System.out.println("Hello, Maven!");. The print statement that prints “Hello, Maven!” to the console when the program runs.

After adding the text, save and exit the file.

Step 4: Build the Project

To build the project, first navigate to the project directory, in this case maven-project1:

cd maven-project1

Once there, run the following to compile the Java source code:

mvn package
mvn package terminal outputmvn package terminal output

Step 5: Execute the Application

After a successful build, execute the application using the following command:

java -cp target/maven-project1-1.0-SNAPSHOT.jar com.example.App
executing application terminal outputexecuting application terminal output

The command launches the Java program (App) from the compiled JAR file (my-maven-project-1.0-SNAPSHOT.jar) using the Java runtime environment.

The output shows Hello, Maven! to indicate the application is working.

Conclusion

After reading this article, you know how to install Maven on Debian via the package manager or the official archive. You have also learned how to create a simple project with Maven.

Next, read the Maven vs. Jenkins comparison.


generate an article with html tags on How to Install Apache Maven on Debian

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?