Book Image

Maven Essentials

By : Russell E Gold, Prabath Siriwardena
5 (1)
Book Image

Maven Essentials

5 (1)
By: Russell E Gold, Prabath Siriwardena

Overview of this book

Maven is the #1 build tool used by developers and it has been around for more than a decade. Maven stands out among other build tools due to its extremely extensible architecture, which is built on of the concept of convention over configuration. It’s widely used by many open source Java projects under Apache Software Foundation, Sourceforge, Google Code, and more. Maven Essentials is a fast-paced guide to show you the key concepts in Maven and build automation. We get started by introducing you to Maven and exploring its core concepts and architecture. Next, you will learn about and write a Project Object Model (POM) while creating your own Maven project. You will also find out how to create custom archetypes and plugins to establish the most common goals in build automation. After this, you’ll get to know how to design the build to prevent any maintenance nightmares, with proper dependency management. We then explore Maven build lifecycles and Maven assemblies. Finally, you will discover how to apply the best practices when designing a build system to improve developer productivity.
Table of Contents (15 chapters)
Maven Essentials
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Installing Apache Maven


Installing Maven on any platform is more than a straightforward task. At the time of writing this book, the latest version was 3.3.3, which is available for download at http://maven.apache.org/download.cgi. This version requires JDK 1.7.0 or above.

Tip

You should keep a note on the Java requirement for version 3.3.3, if you are planning to upgrade from versions 3.0.*, 3.1.*, or 3.2.*. Prior to Maven 3.3.x, the only requirement was JDK 1.5.0. or JDK 1.6.0 (for 3.2.*).

Apache Maven is an extremely lightweight distribution. It does not have any hard requirements on memory, disk space, or CPU. Maven itself is built on top of Java, and it would work on any operating system that runs Java virtual machine (JVM).

Installing Apache Maven on Ubuntu

Installing Maven on Ubuntu is a single line command. Proceed with the following steps:

  1. Run the following apt-get command in the command prompt. You need to have the sudo privileges to execute this:

    $ sudo apt-get install maven
    
  2. This takes a few minutes to complete. Upon the completion of the installation, you can run the following command to verify the installation:

    $ mvn -version
    
  3. You should get an output similar to the following, if Apache Maven has been installed successfully:

    $ mvn -version
    Apache Maven 3.3.3
    Maven home: /usr/share/maven
    Java version: 1.7.0_60, vendor: Oracle Corporation
    Java home: /usr/lib/jvm/java-7-oracle/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.13.0-24-generic", arch: "amd64", family: "unix"
    
  4. Maven is installed under the /usr/share/maven directory. To check the directory structure behind the Maven installation directory, use the following command:

    $ ls /usr/share/maven
    bin  boot  conf  lib  man
    
  5. Maven configuration files can be found at /etc/maven, which can be listed using the following command:

    $ ls /etc/maven
    m2.conf  settings.xml
    

If you don't want to work with the apt-get command, there is another way of installing Maven under any Unix-based operating system. We will discuss this in the next section. Since Mac OS X has a kernel built at the top of the Unix kernel, installing Maven on Mac OS X would be the same as installing it on any Unix-based operating system.

Installing Apache Maven on Mac OS X

Most of the OS X distributions prior to OS X Mavericks had Apache Maven preinstalled. To verify that you've got Maven installed in your system, try out the following command. If it does not result in a version, then it means you do not have it installed:

$ mvn –version

The following steps will guide you through the Maven installation process on Max OS X Yosemite:

  1. First, we need to download the latest version of Maven. Throughout this book, we will use Maven 3.3.3, which was the latest version at the time of writing this book. Maven 3.3.3 ZIP distribution can be downloaded from http://maven.apache.org/download.cgi.

  2. Unzip the downloaded ZIP file into /usr/share/java. You need to have the sudo privileges to execute this:

    $ sudo unzip  apache-maven-3.3.3-bin.zip -d /usr/share/java/
    
  3. In case you already have Maven installed in your system, use the following command to unlink. /usr/share/maven is only a symlink to the directory where Maven is installed:

    $ sudo unlink /usr/share/maven
    
  4. Use the following command to create a symlink to the latest Maven distribution that you just unzipped. You need to have the sudo privileges to execute this:

    $ sudo ln -s /usr/share/java/apache-maven-3.3.3  /usr/share/maven
    
  5. Use the following command to update the value of the PATH environment variable:

    $ export PATH=$PATH:/usr/share/maven/bin
    
  6. Use the following command to update (or set) the value of the M2_HOME environment variable:

    $ export M2_HOME=/usr/share/maven
    
  7. Verify the Maven installation with the following command:

    $ mvn -version
      Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06;   2015-04-22T04:57:37-07:00)
      Maven home: /usr/share/maven
      Java version: 1.7.0_75, vendor: Oracle Corporation
      Java home:   /Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Hom  e/jre
      Default locale: en_US, platform encoding: UTF-8
      OS name: "mac os x", version: "10.10.2", arch: "x86_64",   family: "mac"
    
  8. If you get the following error while running the preceding command, it means you have another version of Maven running in your system, and the PATH system variable includes the path to its bin directory. If that is the case, you need to clean out the value of the PATH system variable by removing the path to the old Maven installation:

      -Dmaven.multiModuleProjectDirectory system property is not   set. Check $M2_HOME environment variable and mvn script match.
    

Note

Maven can also be installed on Mac OS X with Homebrew. This video explains the installation process in detail—

https://www.youtube.com/watch?v=xTzLGcqUf8k

Installing Apache Maven on Microsoft Windows

First, we need to download the latest version of Maven. Apache Maven 3.3.3 ZIP distribution can be downloaded from http://maven.apache.org/download.cgi. Then, we need to perform the following steps:

  1. Unzip the downloaded ZIP file into the C:\Program Files\ASF directory.

  2. Set the M2_HOME environment variable and point it to C:\Program Files\ASF\apache-maven-3.3.3.

  3. Verify the Maven installation with the following command on the command prompt:

    mvn –version
    

Note

To know more about how to set the environment variables on Microsoft Windows, refer to http://www.computerhope.com/issues/ch000549.htm.