Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Apache Maven Cookbook

You're reading from   Apache Maven Cookbook Over 90 hands-on recipes to successfully build and automate development life cycle tasks following Maven conventions and best practices

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781785286124
Length 272 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started 2. IDE Integration with Maven FREE CHAPTER 3. Maven Lifecycle 4. Essential Maven Plugins 5. Dependency Management 6. Code Quality Plugins 7. Reporting and Documentation 8. Handling Typical Build Requirements 9. Multi-module Projects 10. Java Development with Maven 11. Advanced Maven Usage Index

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:

Getting ready

You can also verify the program folder structure from Microsoft Windows Explorer:

Getting ready

How to do it...

Let's start installing Java and Maven by performing the following steps:

  1. Set the variable 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

    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.

  2. 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.

  3. Go to http://maven.apache.org/ and click on the Download link.
  4. The links to the latest stable versions of Maven are displayed.
  5. The binaries are available in both, .zip and .tar.gz formats. Choose one of them.
  6. 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.

  7. Ensure the contents are similar to the following screenshot:
    How to do it...

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 default settings.xml file used by Maven.
  • The 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:

  1. Open Control Panel:
    How to do it...
  2. Choose Advanced system settings:
    How to do it...
  3. Click on Environment Variables.... Add the M2_HOME variable and set it to the folder where Maven was extracted.
    How to do it...
  4. Edit the PATH variable to include Maven's bin folder:
    How to do it...

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:

  1. Open a command prompt in Microsoft Windows and run the following command:
    C:\software\apache-maven-cookbook>mvn -version
    
  2. 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
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image