Environment Setup for Maven

 

Setting up the Maven environment is a crucial step for managing Java-based projects efficiently. Below is a detailed step-by-step guide to install and set up Apache Maven on WindowsLinux, and macOS.


1. Prerequisites for All OS

Before you install Maven:

  • Java (JDK) must be installed.

  • Ensure the JAVA_HOME environment variable is set.

How to Check java installed on machine:


java -version
javac -version


If not installed, download Java JDK from: https://www.oracle.com/java/technologies/javase-downloads.html or use OpenJDK (https://jdk.java.net/)


Windows Installation Steps

Step 1: Download Maven


Step 2: Extract and Place Maven

  • Extract it to a folder (example: C:\Program Files\Apache\Maven)


Step 3: Set Environment Variables

  1. Search for “Environment Variables” in Start Menu.

  2. Under System Variables:

    • Add MAVEN_HOME:

Variable Name: MAVEN_HOME
Variable Value: C:\Program Files\Apache\Maven\apache-maven-3.9.6

Edit the Path variable and add:

%MAVEN_HOME%\bin


Ensure JAVA_HOME is set:

If not, add:


Variable Name: JAVA_HOME
Variable Value: path to JDK (e.g., C:\Program Files\Java\jdk-17)

Step 4: Verify Installation


mvn -v


Linux Installation Steps:

Step 1: Install Java


sudo apt update
sudo apt install openjdk-17-jdk


Set JAVA_HOME:


echo "export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))" >> ~/.bashrc
source ~/.bashrc


Step 2: Download Maven


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


Step 3: Extract and Move


tar -xvzf apache-maven-3.9.6-bin.tar.gz
sudo mv apache-maven-3.9.6 /opt/maven


Step 4: Set Environment Variables
Edit ~/.bashrc or ~/.zshrc:



export MAVEN_HOME=/opt/maven
export PATH=$MAVEN_HOME/bin:$PATH

Apply changes:


source ~/.bashrc


Step 5: Verify Installation


mvn -v


macOS Installation Steps

Option 1: Using Homebrew (Recommended)



brew install maven


Option 2: Manual Installation

Step 1: Install Java

Use brew install openjdk or download from Oracle.

Step 2: Download Maven

Download from https://maven.apache.org/download.cgi

Step 3: Extract and Move

tar -xvzf apache-maven-3.9.6-bin.tar.gz
sudo mv apache-maven-3.9.6 /opt/maven

Step 4: Set Environment Variables

Edit your shell profile (~/.zshrc~/.bash_profile, or ~/.bashrc):


export JAVA_HOME=$(/usr/libexec/java_home)
export MAVEN_HOME=/opt/maven
export PATH=$MAVEN_HOME/bin:$PATH


Apply changes:


source ~/.zshrc  # or relevant shell file


Step 5: Verify


mvn -v


Output:


Apache Maven 3.9.6
Java version: 17.0.1
Java home: C:\Program Files\Java\jdk-17
Default locale: en_US
OS name: "windows 10", version: "10.0", arch: "amd64"