Installing Maven for Java on macOS

Here’s a step-by-step guide to install Apache Maven for Java development on macOS:

Prerequisites

  • Java JDK (Maven requires Java to run)

1. First, check if Java is installed

1java -version

If Java isn’t installed, install it first (JDK 8 or later recommended). You can get it from:

Installation Methods

1# Update Homebrew
2brew update
3
4# Install Maven
5brew install maven
6
7# Verify installation
8mvn -v

Method 2: Manual Installation

  1. Download Maven from the official website
  2. Extract the archive to your desired location (e.g., /usr/local/apache-maven)
  3. Configure environment variables:
1# Edit your shell configuration file (~/.zshrc or ~/.bash_profile)
2nano ~/.zshrc  # or ~/.bash_profile for older macOS versions
3
4# Add these lines (adjust path if needed):
5export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.6
6export PATH=$M2_HOME/bin:$PATH
7
8# Save and reload
9source ~/.zshrc  # or source ~/.bash_profile

Verify Installation

1mvn -version

You should see output similar to:

Apache Maven 3.8.6 (...)
Maven home: /usr/local/apache-maven/apache-maven-3.8.6
Java version: 17.0.2, vendor: Oracle Corporation, runtime: ...

Creating a Maven Project

1mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will create a simple Java project structure in a new my-app directory.

Updating Maven

With Homebrew:

1brew upgrade maven

Manual installation: Download the new version and update your paths.