Maven Installation: A Comprehensive Guide with Examples

Anshul
2 min readMay 6, 2023

--

Maven is a powerful build automation and dependency management tool widely used in Java-based projects. It simplifies building and managing software projects by providing a structured approach and a consistent build process. This article will walk you through the step-by-step process of installing Maven on different operating systems, along with practical examples.

Prerequisites

Before we begin with the installation process, ensure that you have the following prerequisites:

  1. A compatible Java Development Kit (JDK) is installed on your system.
  2. A working internet connection to download Maven.

Maven: Empowering Java Developers with Effortless Build and Dependency Management
Jenkins Masterclass: Continuous Integration and Deployment

Installation Steps

Step 1: Download Maven

To get started, visit the official Apache Maven website (https://maven.apache.org) and navigate to the “Download” section. Look for the latest stable version of Maven and download the binary zip file appropriate for your operating system.

Step 2: Extract the Maven Archive

Once the Maven binary zip file is downloaded, extract its contents to a suitable location on your system. This extracted directory will be called the “Maven Home” throughout this guide.

Step 3: Configure Environment Variables

To use Maven from any location in your system, you need to configure the environment variables correctly.

Windows

  1. Open the Start menu, right-click on “This PC” (or “My Computer”), and select “Properties.”
  2. Click on “Advanced system settings” on the left-hand side to open the System Properties window.
  3. In the System Properties window, click on the “Environment Variables” button.
  4. In the “System variables” section, click on the “New” button.
  5. Enter “MAVEN_HOME” as the variable name and the path to the Maven Home directory (e.g., C:\apache-maven-3.8.1) as the variable value.
  6. Find the “Path” variable in the “System variables” section, select it, and click on the “Edit” button.
  7. Append %MAVEN_HOME%\bin to the existing variable value, separated by a semicolon (;).
  8. Click “OK” to save the changes and close all the windows.

macOS / Linux

  1. Open a terminal window.
  2. Run the command nano ~/.bash_profile to open the Bash profile configuration file.
  3. Add the following line at the end of the file:
export MAVEN_HOME=/path/to/maven
export PATH=$MAVEN_HOME/bin:$PATH

Replace /path/to/maven with the actual path to your Maven Home directory.

5. Press Ctrl + X, then Y, and Enter to save and exit the file.

6. Run the command source ~/.bash_profile to apply the changes to the current terminal session.

Step 4: Verify the Installation

To ensure that Maven is installed correctly, open a new terminal or command prompt window and run the following command:

mvn --version

If Maven is installed properly, you should see the Maven version and other related information displayed in the terminal.

Examples

Now that Maven is installed, let’s explore a few practical examples to demonstrate its usage.

Example 1: Creating a New Maven Project

  1. Open a terminal or command prompt window.
  2. Navigate to the directory where you want to create your Maven project.
  3. Run the following command to create a new Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command generates a new Maven project based on the “maven-archetype

--

--

Anshul

DevRel 🥑 DevOps / Cloud Engineer | Terraform, Ansible, Docker & Kubernetes Enthusiast 🐳 GCP | Jenkins | Terraform Certified