Installing Maven on Microsoft Windows
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.
Getting ready
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:
How to do it...
Let's start installing Java and Maven by performing the following steps:
- Set the variable
JAVA_HOME
to point to the Java installation that you want Maven to use; for example, you can do this by settingJAVA_HOME
variable in the following way:C:\projects\apache_maven_cookbook>set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_20
Note
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. - If JDK is not installed on your system, now is the time to download and install it from the Oracle Java SE download page at http://www.oracle.com/technetwork/java/javase/downloads/index.html.
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.
- Go to http://maven.apache.org/ and click on the Download link.
- The links to the latest stable versions of Maven are displayed.
- The binaries are available in both,
.zip
and.tar.gz
formats. Choose one of them. - Extract the downloaded binary to a folder you want Maven to reside in. In this case I have chosen
C:\software
.Tip
It is best to avoid folders with spaces as some features of Maven or its plugins might not work.
- Ensure the contents are similar to the following screenshot:
The preceding screenshot displays a list of directories contained in Maven.
Now let's briefly discuss what these directories contain:
- The
bin
folder contains the batch files and shell scripts to run Maven on various platforms. - The
boot
folder contains the jars required for Maven to start. - The
conf
folder contains the defaultsettings.xml
file used by Maven. - The
lib
folder contains the libraries used by Maven. It also contains anext
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:
- Open Control Panel:
- Choose Advanced system settings:
- Click on Environment Variables.... Add the
M2_HOME
variable and set it to the folder where Maven was extracted. - Edit the
PATH
variable to include Maven'sbin
folder:
How it works...
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
.
There's more...
Verify your Maven installation using the following steps:
- Open a command prompt in Microsoft Windows and run the following command:
C:\software\apache-maven-cookbook>mvn -version
- The following output should be displayed:
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"
See also
- The Creating a simple project with Maven recipe in this chapter