Maven for Eclipse

5 (1 reviews total)
By Sanjay Shah
    Advance your knowledge in tech with a Packt subscription

  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Apache Maven – Introduction and Installation

About this book

Starting with an exploration of the Maven architecture and its installation, you will then learn how to install m2eclipse, which provides Maven integration with Eclipse. Furthermore, the book guides you through the stages of project creation, including building, testing, and executing the projects.

You will learn to customize your projects by demonstrating different core concepts of the Maven project structure. Nevertheless, you will also become familiar with the build lifecycles that generate the required artifacts. Moreover, it will also guide you through the process of handling multimodule projects and working with them effectively.

By the end of this book, you will have a good understanding of m2eclipse and will be able to use it efficiently with ease.

Publication date:
August 2014


Chapter 1. Apache Maven – Introduction and Installation

"A journey of a thousand miles starts with a single step", Lao Tzu. Rightly so, if you are reading this sentence here, you have taken a step towards a journey of Maven with Eclipse. As part of this journey, in the very first chapter, we will introduce you to Maven and its basic architecture and then guide you through the installation process through the following subtopics:

  • Introduction to Maven

  • Maven's origin

  • Maven's principles

  • Maven's component architecture

  • Maven versus Ant

  • Downloading Maven

  • Installing Maven

  • Installing Maven on Windows

  • Installing Maven on Linux and Mac OS

  • Verifying the installation of Maven


Introduction to Maven

Apache Maven's official site states that Apache Maven, which is also known as Maven, is a software project management and comprehension tool. Generally, software project management comprises planning, organizing, managing resource pools, and developing resource estimates; hence, it is a meaningless abstraction to justify Maven offerings. To put it in simple words, Maven is a comprehensive approach towards the process of applying patterns to a build infrastructure with primary goals as follows:

  • Easing the build process

  • Providing a uniform build system

  • Providing quality project information

  • Providing guidelines for best practice development

  • Allowing transparent migration to new features

In order to achieve the preceding goals, Maven provides a set of build standards, an artifact repository model, an engine that describes projects, and a standard lifecycle to build, test, and deploy project artifacts.

Maven's origin

Maven, a Yiddish word that means accumulator of knowledge, was initially started as an attempt to simplify the build processes in the Jakarta Turbine project. Prior to Maven, Ant was the build tool used across projects, and there were different Ant build files across different projects. Also, there were no standard or consistent Ant build files for projects, and JARs were also required to be checked in subversion. Hence, there was a growing necessity to standardize the project's build process and its structure, publish project information, and reuse JARs across projects, which resulted in the formation of a new tool, Maven. Maven has made the day-to-day work of developers easy, and it provides comprehension of any Java project.

Maven's principles

Maven's principles can be stated in the following points:

  • Convention over configuration: Maven defines the default project structure and builds a life cycle that eases the burden during development. By specifying a publicly defined model, it makes the project more understandable.

  • Declarative execution: Maven defines a build life cycle that comprises phases, which in turn are made up of plugin goals. Plugins and goals can be included declaratively in the pom file to customize the execution of a project.

  • Reusability: Maven was built with reusability in mind. The build and execution declaration in one project can be used across different projects. Maven also makes it easier to create a component and integrate it into a multiproject build system. Also, with Maven Best Practices, development across the industry is encouraged.

  • Coherent organization of dependency: Maven takes care of dependency management, thus reducing the burden on the part of developers. Different conflicts across dependencies are also handled beautifully.

  • Focus on writing applications: With a standard project layout and build lifecycle, there is no need to develop the build; the focus should primarily be on building the application.

Maven's component architecture

Maven is built around different components as shown in the following diagram:

Maven component architecture (Reference Apache Team Presentation)

The Plexus container

Plexus is an IOC container that provides component-oriented programming to build modular, reusable components that can be easily assembled and reused. Some of the features supported are as follows:

  • Component lifecycles

  • Component instantiation strategies

  • Nested containers

  • Component configuration

  • Auto-wiring

  • Component dependencies

  • Various dependency injection techniques, including constructor injection, setter injection, and private field injection


More information on this can be found at


Maven Wagon is a transport abstraction used in the Maven artifact and repository-handling code. Wagon defines a unified API, and it currently has the following providers:

  • File

  • HTTP

  • HTTP lightweight

  • FTP


  • WebDAV


More information on this can be found at

Maven Doxia

Doxia is a content generation framework that provides users with powerful techniques to generate static and dynamic content. Doxia is also used in a web-based publishing context to generate static sites, in addition to being incorporated into dynamic content generation systems such as blogs, wikis, and content management systems.


For more information on Maven Doxia, refer to


The Modello component in Maven can be used to generate the following types of artifacts at build time with reference to the data model:

  • Java POJOs of the data model

  • Java POJOs to XML

  • XML to Java POJOs

  • Xdoc documentation of the data model

  • XML schema to validate that XML content matches the data model


For more information, refer to

Maven SCM

This component provides a common API to perform Source Code Management (SCM) operations. The following type of SCMs are supported:

  • Bazaar

  • CVS

  • Git

  • Jazz

  • Mercurial

  • Perforce

  • StarTeam

  • Subversion

  • CM energy


More information is available at


Maven versus Ant

Before the emergence of Maven, Ant was the most widely used build tool across Java projects. Ant emerged from the concept of creating files in C/C++ programming to a platform-independent build tool. Ant used XML files to define the build process and its corresponding dependencies.

Another Neat Tool (Ant) was conceived by James Duncan Davidson while preparing Sun's reference JSP/Servlet engine, Apache Tomcat. The following is a simple sample of an Ant build file (

<project name="MyProject" default="dist" basedir=".">
        simple example build file
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>


Downloading the sample code

You can download the sample code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

This example shows how to build a simple JAR file. Note how all the details corresponding to source files, class files, and JAR files have to be specified. Even the sequence of steps must be specified. This results in a complex build file and often a lot of duplicated XML.

Let's look at the simplest Maven build file, the pom file, which will be discussed in more detail in Chapter 3, Creating and Importing Projects.

A simple pom file will look as shown in the following code snippet:


This is all we need to build and package as a JAR from a Java project. Some of the differences between Ant and Maven in the preceding examples are as follows:

  • Convention over configuration: Ant requires a developer to configure everything right from the source code's location to the storage of a JAR file. Maven, on the other hand, follows conventions, has a well-defined project structure, and knows where to reference source, resource files, and place the output.

  • Lifecycle: Ant does not have a lifecycle and requires defining goals and their dependencies. Also, in Ant, the sequence of tasks needs to be specified. Maven has defined a lifecycle that consists of build phases and goals; hence, no configuration is required.

Apart from the preceding differences that can be cited from the preceding simple example, Maven is superior to Ant in the following aspects:

  • Higher level of reusability: The build logic can be reused with Maven across different projects in Maven.

  • Less maintenance: With a standardized structure and the reusability option, it requires less effort towards maintenance.

  • Dependency management: One of the most superior aspects of Maven over Ant is its ability to manage the corresponding dependencies. Though, lately, Ant in combination with Apache Ivy does ease dependency management; however, Maven has other aspects that outdo this combo offering.

  • Automatic downloads: Maven downloads the dependencies automatically; however, Ant lacks this. While Ant can use Ivy to replicate this behavior, it requires additional behavior.

  • Repository management: Maven repositories are arbitrary and accessible locations that are designed to store the artifacts that Maven builds. They manage repositories as local versus remote (will be discussed in detail in the Repository section of Chapter 3, Creating and Importing Projects). Ant does not have this aspect built.


Downloading Maven

To download Maven, please visit Click on the latest version,; at the time of writing this, the current version is Download the latest version as shown in the following screenshot:

Once the ZIP file is downloaded, extract the files to, let's say, maven3. After extraction, the contents of the maven3 folder will have another folder named apache-maven-3.2.1 and the contents of that folder will be as shown in the following screenshot:


Installing Maven

Before we install Maven, we need to have JDK installed. Check out the Java installation with the following command:

>javac -version

For Windows, open the command prompt, and for Linux/Mac OS, open the terminal and use the preceding command to see the version of the JDK that is installed.

If JDK is not installed, please refer to following link and install it:

Once Java is in place, let's move towards Maven's installation.

Maven's installation is a simple two-step process:

  • Setting up Maven home, that is, the M2_HOME variable

  • Adding Maven home to the PATH variable

Installing Maven on Windows

The installation of Maven is just setting up Maven home in the extracted Maven folder. For ease, let's assume the maven3 folder resides in C:\Program Files. Now, set Maven home with the following command in the command prompt:

set M2_HOME="c:\Program Files\maven3\apache-maven-3.2.1"

Update the PATH variable as follows:

set PATH =%PATH%;%M2_HOME%\bin

Alternatively, the variables can be set permanently by navigating to Desktop | My Computer | Properties. Visit for more information.

Installing Maven on Linux and Mac OS

Let's assume the maven3 folder resides in the /opt folder. As Mac OS does not have the /opt folder, let's create a folder opt in root, that is, /opt. Now, let's assume we have maven3, the extracted folder in it. Then, set the Maven home by issuing the following command via the terminal:

export  M2_HOME=/opt/maven3/apache-maven-3.2.1

Add Maven to the PATH variable as follows:

export PATH=${M2_HOME}/bin:${PATH}

To add it permanently, add it to the bash file as follows:

cd $HOME
vi .bash_profile

Add the preceding variable, that is, two lines to the file, save it, and execute the following command:

source  .bash_profile

Verifying the installation of Maven

After performing the previous steps, its time to verify the installation of Maven. To verify the installation, perform the following:

  • For Windows, open the command prompt and type the following:

    mvn -version
  • For Linux and Mac OS, open the terminal and type the following:

    mvn -version

It should show the corresponding version of Maven installed, as shown in the following screenshot:



Congratulations! By the end of this chapter, you have got yourselves acquainted with Maven and have installed Maven in your system. Now you are ready to take a sprint towards the journey. In the next chapter, you will learn about installing and setting up the m2eclipse plugin for Eclipse.

About the Author

  • Sanjay Shah

    Sanjay Shah has more than 9 years of experience of working in diverse areas of application development across mobile and web platforms. He is currently working as a software architect and has a number of enterprise applications to his name.

    He is the co-author of the book Android Development Tools for Eclipse, Packt Publishing, also co-authored by Khirulnizam Abd Rahman.

    Along with being a literature enthusiast, he is fond of philosophy and enjoys life in Nepal, the land of the highest peak in the world, Mt. Everest.

    Browse publications by this author

Latest Reviews

(1 reviews total)

Recommended For You

Book Title
Unlock this book and the full library for FREE
Start free trial