
Apache Maven Cookbook
By :

At the time of writing this book, Microsoft Windows 8.1 is the latest version of Microsoft Windows. While the screenshots and output will be for Microsoft Windows 8.1, the steps are similar for earlier (and possibly later) versions as well.
As Maven requires a Java platform, first ensure that you have installed the Java environment on your system, Java Development Kit (JDK) specifically; Java Runtime Environment (JRE) is not sufficient.
You can verify whether Java is installed on your system by opening Add or Remove Programs. If you see something similar to the following screenshot, JDK is installed on your system:
You can also verify the program folder structure from Microsoft Windows Explorer:
Let's start installing Java and Maven by performing the following steps:
JAVA_HOME
to point to the Java installation that you want Maven to use; for example, you can do this by setting JAVA_HOME
variable in the following way:C:\projects\apache_maven_cookbook>set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_20
Note that this setting will not be persisted once the command prompt is closed. To persist this, set Environment Variables... using the Control Panel option, as described later for the M2_HOME
variable.
Once it is installed, ensure JAVA_HOME
is set as described earlier.
Now that we have set up Java, let us download and set up Maven.
.zip
and .tar.gz
formats. Choose one of them.C:\software
.It is best to avoid folders with spaces as some features of Maven or its plugins might not work.
The preceding screenshot displays a list of directories contained in Maven.
Now let's briefly discuss what these directories contain:
bin
folder contains the batch files and shell scripts to run Maven on various platforms.boot
folder contains the jars required for Maven to start.conf
folder contains the default settings.xml
file used by Maven.lib
folder contains the libraries used by Maven. It also contains an ext
folder in which third-party extensions, which can extend or override the default Maven implementation, can be placed.Now let us make sure we can run Maven from the command prompt by carrying out the following steps:
M2_HOME
variable and set it to the folder where Maven was extracted.PATH
variable to include Maven's bin
folder:A Maven installation is essentially a set of JAR files, configuration files, and a Microsoft Windows batch file, mvn.bat
.
The mvn
command essentially runs this batch file. It first checks for JAVA_HOME
. This file is present in the bin
folder of the Maven installation and, hence, it needs to be in PATH
.
If the batch file does not find JAVA_HOME
, it looks for Java
in its PATH
. This can lead to unexpected results, as typically the Java
in PATH
is usually the JRE and not the JDK.
The batch file then looks for M2_HOME
, which is the location of the Maven installation. It does this so that it can load the libraries that are present.
Additionally, it also reads values specified in MAVEN_OPTS
. This variable allows you to run Maven with an additional heap size and other Java parameters.
Using the values for JAVA_HOME
, M2_HOME
, and Maven_OPTS
, the batch file runs its main class org.codehaus.plexus.classworlds.launcher.Launcher
.
Verify your Maven installation using the following steps:
C:\software\apache-maven-cookbook>mvn -version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:59:23+05:30)
Maven home: C:\software\apache-maven-3.2.5
Java version: 1.7.0_67, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_67\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"