fbpx
How to Install Apache Maven on Ubuntu

This tutorial aims to provide a detailed, step-by-step guide on how to install Apache Maven on Ubuntu, a popular open-source operating system. In the realm of software development, Apache Maven stands as a robust project management tool, widely embraced for its ability to handle project builds, reporting, and documentation from a central piece of information. It simplifies and standardizes the project build process and provides guidelines for best practices development.

Apache Maven and Its Importance

The Project Object Model (POM) concept serves as the foundation for Apache Maven, also known as Maven, which is a potent tool for managing and understanding software projects. The build process’s simplicity and the provision of a standardized build system are Maven’s main objectives. The following management options are provided to developers:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution

Maven is a crucial tool in contemporary software development because of its extensive feature set, especially for tasks requiring a standardized and repeatable build system.

Pre-Installation Requirements

Before proceeding with the installation of Apache Maven on Ubuntu, ensure that you have:

  • A Virtono VPS running Ubuntu OS (preferably the latest version)
  • A user account with sudo privileges
  • Installing Java Development Kit (JDK)

Your system must have JDK installed in order to use Apache Maven on Ubuntu. How to install JDK on Ubuntu is detailed below:

Update the Package List

Before installing any package, it’s advisable to update your package list. Use the following command:

sudo apt update && apt upgrade -y

Install JDK

Install the default JDK package using the command:

sudo apt install default-jdk -y
Install Apache Maven on Ubuntu

Verify Java Installation

To ensure Java is installed correctly, check the installed version:

java -version

The system should display the installed version of Java.

How to Install Apache Maven on Ubuntu

Installing Apache Maven on Ubuntu

Now, let’s proceed with Apache Maven’s installation.

Step 1: Download Apache Maven

Go to the official Apache Maven page and copy the URL for the most recent version. Then, download it using the ‘wget’ command.

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

Step 2: Extract the Downloaded Archive

After downloading, use the ‘tar’ command to extract the archive:

tar xzf apache-maven-3.9.4-bin.tar.gz

Step 3: Move Apache Maven

Move the extracted directory to ‘/opt’ directory, which is used for storing third-party software on Unix systems:

sudo mv apache-maven-3.9.4 /opt/apache-maven

Step 4: Set Up Environment Variables

Set up the environment variables by editing the ‘/etc/profile.d/mavenenv.sh’ file:

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

Add the following lines to the file:

export M2_HOME=/opt/apache-maven
export PATH=${M2_HOME}/bin:${PATH}

Save and close the file. Make it executable with the following command:

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

Then, load the environment variables using the source command:

source /etc/profile.d/mavenenv.sh

Verifying Apache Maven Installation

To verify if Apache Maven on Ubuntu is installed correctly, use the following command:

mvn -version

The system should display the installed version of Apache Maven, similar to this:

Apache Maven on Ubuntu version

How to use Apache Maven on Ubuntu

Once you have Apache Maven on Ubuntu installed, you can start using it for your projects. Here are some basic commands and examples to get you started:

Creating a New Project

Maven uses archetypes which are project templates to create new projects. To create a new project, use the mvn archetype:generate command. For example:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Apache Maven on Ubuntu New Project

Building the Project

Navigate to the directory where the pom.xml file of your project is located. You can build the project using the mvn package command:

cd /path/to/your/project
mvn package

This command compiles the source code of the project and packages the result into a JAR file.

Building the Project on Apache Maven on Ubuntu

Running the Project

If your project is a simple Java application, you can run it using the java -cp command. For example:

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

Here, com.mycompany.app.App is the main class defined in your project.

Running the Project Apache Maven on Ubuntu

Checking the Status

Maven does not have a built-in command to check the status of a project. However, you can use the mvn validate command to check if the project is correct and all necessary information is available. This command also validates if the project is structured correctly.

mvn validate
Checking the Status Apache Maven on Ubuntu

Cleaning the Project

The mvn clean command cleans the project and removes all files generated by the previous build:

mvn clean
Cleaning the Project Apache Maven on Ubuntu

Testing the Project

Maven can also run tests using the mvn test command:

This command executes the unit tests of the project using the testing framework you have configured.

Testing the Project Apache Maven on Ubuntu

Final Thoughts

Congratulations! Apache Maven on Ubuntu is now properly installed. The procedure may appear difficult at first, but with practice it becomes simple. You can now use Apache Maven to better manage and understand your software projects.


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.