How to install maven on Windows and Linux/Unix

Maven is a java-based project build tool that automates the build process. To install Maven on any platform, you’ll need a Java development environment. Before installing maven, make sure you have the most recent JDK version installed.

You can check my other posts on maven commands

Install and setup maven on local windows

Download the Maven software from the Apache site, extract the files, and then copy it to a local drive, for example, D:\java\apache-maven-3.0.3.

Before proceeding, set the following environment variables on your local machine:

Set the M3_HOME variable to the Maven home folder (for example, D:\java\apache-maven-3.0.3). Add the M3_HOME variable to the PATH environment variable (e.g., %PATH%\%M2_HOME%\bin). To confirm that Maven is installed correctly, open the command window and execute the following command:

mvn --version

It shows both the maven and the java versions. The screenshot for this command can be found below.

Install, configure Maven in windows

Install and setup Maven on Linux

First, download the latest version of Maven from the Maven website. Once downloaded, extract the contents from the file named apache-maven-3.5.0-bin.tar.gz to your local folder. Save the extracted files to usr/local/apache-maven-3.5.0. Afterward, set up the Maven environment variables.

export M3_HOME=/usr/local/apache-maven-3.5.0
export PATH=$M3_HOME/bin:$PATH

Verify maven installation using mvn —v command So this is the same process for installation of a maven on Mac and Unix

  • Setup java/JDK
  • Download the Maven library and extract the local folder
  • Setup Environment variables
  • Verify using the mvn —v command

Fix for java.lang.OutOfMemoryError issue in maven

When Maven compiles, copies, and runs integration tests using the mvn install command, problems may arise if your project is large, leading to an excessive number of objects in the heap memory. In such cases, an exception might be thrown if the heap memory struggles to manage the size of the running objects in your project.

Run the below command in the terminal to set the JVM heap size to 512 MB.

MAVEN_OPTS="-Xmx512m -Xms512m -XX:MaxPermSize=512m"