Maven Tutorial - Maven Setup








System Requirements

The following table lists the system requirements for installing Maven.

Item Requirements
JDK Maven 3.2 requires JDK 1.6 or above
Maven 3.0/3.1 requires JDK 1.5 or above
Memory No minimum requirement
Disk 10MB for the Maven installation itself.
At least 500MB for your local Maven repository.
Operating System No minimum requirement.




JDK

Maven is a Java tool, we have to have Java installed first.

We need a Java Development Kit (JDK), the Java Runtime Environment (JRE) is not sufficient.

To verify Java Development Kit version on Windows, open Command Console and type

c:\> java -version

To verify Java Development Kit version on Linus, open Command Terminal and type

$ java -version

To verify Java Development Kit version on Mac, open Terminal and type

$ java -version




Set up JDK

Download JDK from Oracle web site and execute the installation file.

JAVA_HOME environment variable

We have to set the JAVA_HOME environment variable to point to the JDK installation directory on your machine.

On Windows open a console window and issue the following command

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0

On Linux issue the following command

export JAVA_HOME=/usr/local/jdk

On Mac use the following command

export JAVA_HOME=/your_JDK_folder

JDK Path

The next step is to append java and javac file location to System Path.

On Windows append the following string to the end of the system variable Path.

set PATH=C:\Jdk_Folder\bin;%PATH%

On Linux use the following command

export PATH=$PATH:$JAVA_HOME/bin/

Download Maven

Go to http://maven.apache.org/download.cgi to download installation file for your target system.

Extract the Maven archive

To extract the installation archive downloaded from previous step to the directory you wish to install Maven.

OS Location
Windows C:\apache-maven
Linux /usr/local/apache-maven
Mac /usr/local/apache-maven

Set Maven environment variables

In order to use Maven we have to set up the following three environment variables.

Variable Name Description
M2_HOME Install Location. (C:\apache-maven)
M2 For Maven command.
MAVEN_OPTS Maven memory usage setting

On Windows we set the environment variables as follows.

set M2_HOME=C:\apache-maven-install-path
set M2=%M2_HOME%\bin
set MAVEN_OPTS=-Xms256m -Xmx512m

On Linux we set the environment variables as follows.

export M2_HOME=/usr/local/apache-maven-install-path
export M2=$M2_HOME/bin
export MAVEN_OPTS=-Xms256m -Xmx512m

On Mac we set the environment variables as follows.

export M2_HOME=/usr/local/apache-maven-install-path
export M2=$M2_HOME/bin
export MAVEN_OPTS=-Xms256m -Xmx512m

System Path

In order to use Maven command line tools anywhere on your system, we have to add Maven bin directory location to system path.

On Windows append the string ;%M2% to the end of the system variable PATH.

M2 is the variable defined in the step of setting Maven environment variables.

On Linux we can use the following command

export PATH=$M2:$PATH

On Mac we can use the following command

export PATH=$M2:$PATH

Verify Maven installation

To verify the Maven is installed we can open a console window and execute the following Maven command.

c:\Java_Dev>mvn --version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
Maven home: C:\Java_Dev\sdk\apache-maven-3.2.3
Java version: 1.8.0_05, vendor: Oracle Corporation
Java home: C:\Java_Dev\sdk\jdk\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
'cmd' is not recognized as an internal or external command,
operable program or batch file.
c:\Java_Dev>

The following code is from mavenRun.bat which we can use the set up the maven environment.

c:\Java_Dev>type mavenRun.bat
set MAVEN_OPTS=-Xms256m -Xmx512m
set M2_HOME=C:\Java_Dev\sdk\apache-maven-3.2.3
set M2=C:\Java_Dev\sdk\apache-maven-3.2.3\bin

set PATH=%PATH%;C:\Java_Dev\sdk\apache-maven-3.2.3\bin;

Maven Settings File

Maven has two spots to store the settings files.

In the settings files we can configure settings for all Maven POM files. For instance, you can configure location of local repository and active build profile.

The settings files are called settings.xml.

The two settings files are located at:

  • The Maven installation directory: $M2_HOME/conf/settings.xml
  • The user's home directory: ${user.home}/.m2/settings.xml

Both files are optional. If both files are present, the one in the user home settings file overrides the values in the Maven installation settings file.