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
Conferences
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 FREE CHAPTER 2. IDE Integration with Maven 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 Mac OS X

Let us look at the steps to install Maven on Mac OS X. This applies to the latest version of Mac OS X, namely Yosemite.

Earlier, Apple provided Java for Mac, but stopped doing so from Java 7 onwards. Apple Java is not available on recent versions of Mac OS X.

Getting ready

Let us check if the Java environment is available on your Mac:

  1. Open the terminal and run the following command:
    /usr/libexec/java_home -v 1.7
    
  2. See if you get an output similar to the following:
    /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
    
  3. Run the following command to check if you have Java 8:
    /usr/libexec/java_home -v 1.8
    
  4. This should give the following output if Java exists:
    /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home

    As you can see, my system has both Java 1.7 and 1.8.

  5. Set JAVA_HOME to the desired JDK. This can be done in two ways, depending on what you desire:

    If this is for the duration of the session, run the following command:

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
    

    If this is permanent, add the preceding line in .bash_profile in your HOME folder

Tip

Ensure you have the JDK installation and not JRE.

If Java is not present, download and install Java from the Oracle Java download page at http://www.oracle.com/technetwork/java/javase/downloads/index.html.

Once installed, verify the Java installation by following the preceding steps.

Now, let us set up Maven on Mac OS X.

How to do it...

Let's download Maven from its official website by performing the following steps:

  1. Go to http://maven.apache.org/ and click on the Download link. Links to the latest stable versions of Maven will be displayed.
  2. The binaries are available in both .zip and .tar.gz formats. Choose one of them.
  3. Extract the downloaded binary to the folder you want Maven to reside in. The typical location for the Mac is /usr/local folder.
  4. You will need a super user (su) or administrator access to place the contents in the /usr/local folder. If you do not have access, you can place the contents in a subfolder of your HOME folder.
  5. Ensure the contents are similar to the following output by executing the following command:
    /usr/local/apache-maven-3.2.5$ ls -l
    

    The output is shown as:

    total 27
    -rw-r--r--    1 root    wheel    17464 Aug 12 02:29 LICENSE
    -rw-r--r--    1 root    wheel      182 Aug 12 02:29 NOTICE
    -rw-r--r--    1 root    wheel     2508 Aug 12 02:26 README.txt
    drwxr-xr-x    8 root    wheel     4096 Aug 19 13:41 bin
    drwxr-xr-x    3 root    wheel        0 Aug 19 13:41 boot
    drwxr-xr-x    4 root    wheel        0 Oct 14 17:39 conf
    drwxr-xr-x   67 root    wheel    28672 Aug 19 13:41 lib
    
  6. Set the M2_HOME variable as follows:
    export M2_HOME=/usr/local/apache-maven-3.2.5
    
  7. Update the PATH to include Maven's bin folder:
    export PATH=$PATH:$M2_HOME/bin
    

Tip

Like JAVA_HOME, the preceding settings can be persisted by updating .bash_profile with the preceding lines.

In the preceding steps, we discussed the steps to download Maven from its official website. We will now discuss installing Maven using brew. Brew is a popular application on Mac OS X to install open source software. If you have brew installed on your Mac OS X, run the following command to install Maven:

brew install maven

The output for the preceding command will be displayed as shown in the following screenshot:

How to do it...

How it works...

The Maven installation is essentially a set of JAR files, configuration files, and a Mac OS X shell script, namely mvn.

The mvn command essentially runs this script. 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 shell script 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 installation and not JDK.

The shell script then looks for M2_HOME, which is the location for 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 shell script 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 and run the following command:
    mvn –version
    
  2. The output for the preceding command should be displayed as shown in the following screenshot:
    There's more...

See also

  • The Creating a simple project with Maven recipe in this chapter
You have been reading a chapter from
Apache Maven Cookbook
Published in: Apr 2015
Publisher: Packt
ISBN-13: 9781785286124
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