Skip to content

Apache Maven

homepage-banner

Introduction

Apache Maven is a powerful tool for building and managing Java-based projects. It is widely used in the software development industry due to its simplicity and flexibility. In this blog post, we will discuss the basics of Apache Maven and its application in software development.

Understanding Apache Maven

Apache Maven is an open-source project management tool that helps developers in the building, managing, and deploying of software projects. It provides a platform for managing the entire development process, from compiling code to deployment. Maven uses a declarative approach to build projects, where developers specify the project structure and dependencies in a configuration file called a POM (Project Object Model).

Maven makes it easy to manage dependencies, which are external libraries required by the project. It downloads these dependencies automatically from a central repository, making it easier to manage and update them. Maven also provides a consistent way to build projects across different environments, ensuring that the same build process is used for development, testing, and production.

Application of Apache Maven

Maven is widely used in software development due to its numerous benefits. Below are some of the ways in which Apache Maven is applied in software development:

Building and Packaging of Projects

Maven simplifies the process of building and packaging projects. It provides a consistent way of building projects, regardless of the development environment. Maven makes it easier to handle dependencies by automatically downloading them from a central repository. It also creates a standard project structure, making it easier for developers to understand the project layout.

Managing Dependencies

Maven makes it easier to manage dependencies in software projects. It automatically downloads and resolves dependencies from a central repository, saving developers time and effort. It also ensures that the dependencies are up-to-date, reducing the risk of using outdated libraries.

Integration with Continuous Integration (CI) Tools

Maven integrates seamlessly with CI tools like Jenkins, Travis CI, and CircleCI. This integration makes it easy to automate the build process, including running tests and deploying the application. CI tools can automatically build and test the application whenever changes are made to the codebase, ensuring that the application is always in a deployable state.

Installation of Apache Maven

Prerequisites

Maven is implemented in Java, so in order to run Maven, we need a system with Java properly installed and configured. One option is to download an OS-compatible Java JDK from Oracle’s download site. It’s recommended to install it to a path without spaces.

After installing Java, we need to make sure that the commands from the Java JDK are included in our PATH environment variable.

To do this, we can run the command below to get information about the currently installed version:

java -version

Installing Maven on Windows

To install Maven on Windows, go to the Apache Maven site and download the latest version. Select the Maven zip file, for example, apache-maven-3.8.4-bin.zip, and unzip it to the desired folder.

Adding Maven to the Environment Path

Add both M2_HOME and MAVEN_HOME variables to the Windows environment using system properties. Point them to the Maven folder.

Update the PATH variable by appending the Maven bin folder — %M2_HOME%\bin — so that we can run the Maven command from anywhere.

To verify the installation, run:

mvn -version

The command above should display the Maven version, the Java version, and the operating system information. That’s it. Maven is now set up on the Windows system.

Installing Maven on Linux

To install Maven on a Linux operating system, download the latest version from the https://maven.apache.org/download.cgi and select the Maven binary tar.gz file, for example, apache-maven-3.8.4-bin.tar.gz.

Many Linux distributions, including Redhat and Ubuntu, use the BASH shell by default. In the following section, we will use BASH commands.

First, create a location for Maven:

mkdir -p /usr/local/apache-maven/apache-maven-3.8.4

Then, extract the archive to the Maven location:

tar -xvf apache-maven-3.8.4-bin.tar.gz -C /usr/local/apache-maven/apache-maven-3.8.4

Adding Maven to Environment Path

Open the command terminal and edit the .bashrc file using the following command:

nano ~/.bashrc

Next, add Maven-specific lines to the file:

export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.4
export M2=$M2_HOME/bin
export MAVEN_OPTS=-Xms256m -Xmx512m
export PATH=$M2:$PATH

After saving the file, reload the environment configuration without restarting:

source ~/.bashrc

Finally, verify that Maven has been added:

mvn -version

The output should be similar to the following:

Apache Maven 3.8.4 (81a9f75f19aa7275152c262bcea1a77223b93445; 2021-01-07T15:30:30+01:29)
Maven home: /usr/local/apache-maven/apache-maven-3.8.4

Java version: 1.8.0_75, vendor: Oracle Corporation

Java home: /usr/local/java-current/jdk1.8.0_75/jre

Maven has been successfully installed on the Linux system.

Install Maven on Ubuntu Using the Official Website

Enter the following commands to update the package index and install the default OpenJDK package.

sudo apt-get update
sudo apt install default-jdk

java -version

Then run the command sudo apt-get install maven to install the latest Maven:

sudo apt-get install maven

Creating a New Project

To create a new Maven project, you can use the archetype:generate goal. This goal creates a new project using a template that you specify. For example, to create a new web application project, you can run the following command:

mvn archetype:generate -DgroupId=com.example -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

This command will create a new web application project with the group ID com.example and the artifact ID my-webapp.

Building a Project

To build a Maven project, you can use the package goal. This goal compiles the code, runs the tests, and packages the application into a JAR or WAR file. For example, to build the previously created web application project, you can run the following command:

mvn package

This command will compile the code, run the tests, and package the application into a WAR file.

Running Tests

Maven can also be used to run tests. To run tests, you can use the test goal. This goal compiles the code and runs the tests. For example, to run the tests for the previously created web application project, you can run the following command:

mvn test

This command will compile the code and run the tests.

Conclusion

Apache Maven is a powerful tool for building and managing Java-based projects. It simplifies the process of building and packaging projects, managing dependencies, and integrating with CI tools. By using Apache Maven, developers can focus on writing high-quality code, while Maven handles the rest of the project management process.

References

  • https://maven.apache.org/
Leave a message