Introduction of Maven – Complete Guide

maven-introduction

Contents
What is Maven?
Philosophy of Maven
Benefits of Maven
High Level Features and Benefits of Maven
Download Maven
Installation Instructions
Windows 2000/XP
Unix-based Operating Systems (Linux, Solaris and Mac OS X)
Optional configuration
Settings
Settings Reference
First Maven project?

What is Maven?

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
At first glance Maven can appear to be many things, but in a nutshell Maven is an attempt to apply patterns to a project’s build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices. Maven is essentially a project management and comprehension tool and as such provides a way to help with managing:

  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution

Philosophy of Maven

Maven is generally considered by many to be a build tool. Many people who come to Maven initially are familiar with Ant so it’s a natural association but Maven is not just a build tool, and not just a replacement for Ant. Maven is an entirely different creature from Ant. Ant is simply a toolbox whereas Maven is about the application of patterns in order to achieve an infrastructure which displays the characteristics of visibility, reusability, maintainability, and comprehensibility.
Maven was borne of the very practical desire to make several projects at Apache work in the same way. So that developers could freely move between these projects, knowing clearly how they all worked by understanding how one of them worked. If a developer spent time understanding how one project built it was intended that they would not have to go through this process again when they moved on to the next project. The same idea extends to testing, generating documentation, generating metrics and reports, testing and deploying.

Benefits of Maven

  • Standardized project layout and project structure generator.
  • Standardized dependency-management mechanism.
  • Multiple project support.
  • Instant downloads of new plugins and features as the developer needs them.
  • Website generation for up-to-date project information.
  • Integration with source control: CVS and Subversion.
  • Reusability
  • scalability (lower level of additional info/code to add a new step to the build process)
  • Build lifecycle management
  • Large existing repository
  • Eclipse aware
  • Well documented (hopefully soon)
  • One directory layout,
  • A single way to define dependencies,
  • Setting up a project is really fast,
  • 99% of my needs are available out of the box,
  • build best practices enforcement (shared build meme)
  • automated build of application, from source code to pre-production platform => fast time to market with reduced risks
  • Works well with distributed teams 😉 Location doesn’t matter.
  • All artifacts are versioned and store in a repository
  • Build process is standardized for all projects
  • A lot of goals are available so it isn’t necessary to develop some specific build process part contrary to ANT we can reuse existing ANT tasks in build process with ant run plug-in
  • it provide quality project information with generated site
  • Easy to learn and use
  • Makes the build process much easier at the project level (i.e. don’t have to create very much for each project for Maven to build it correctly, and those things you do create are more declarative than functional)
  • Automatic project web sites with consistent information about each project
  • Recommended standards and best practices for project layout and definition
  • Promotes modular design of code. by making it simple to manage mulitple projects it allows the design to be laid out into multiple logical parts, weaving these parts together through the use of dependency tracking in pom files.
  • Enforces modular design of code
  • Quick project setup, no complicated build.xml files, just a POM and go

 

High Level Features and Benefits of Maven

  • Encourages best practices
  • Provides a uniform build system and consistent usage across all projects
  • Provides dependency management including automatic updating, dependency closures (also known as transitive dependencies)
  • Provides reuse of build logic
  • Defines project standard directory layout
  • Helps to reduce the duplication of dependent software libraries (jars) required to build an application
  • Stores all the software libraries or artifacts in a remote stores called a Maven repositories

Download Maven

Maven is distributed in several formats for your convenience. Maven 2.2.1 is distributed under the Apache License, version 2.0.
Download link: http://maven.apache.org/download.html

Installation Instructions

System Requirements
JDK 1.5 or above (this is to execute Maven – it still allows you to build against 1.3 and prior JDK’s)
Memory No minimum requirement
Disk No minimum requirement. Approximately 100MB will be used for your local repository, however this will vary depending on usage and can be removed and redownloaded at any time.
Operating System No minimum requirement. On Windows, Windows NT and above or Cygwin is required for the startup scripts. Tested on Windows XP, Fedora Core and Mac OS X.
Maven is a Java tool, so you must have Java installed in order to proceed. More precisely, you need a Java Development Kit (JDK), the Java Runtime Environment (JRE) is not sufficient.
Additional optional installation steps are listed after the platform specific instructions.

Windows 2000/XP

  • Unzip the distribution archive, i.e. apache-maven-2.2.1-bin.zip to the directory you wish to install Maven 2.2.1. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-2.2.1 will be created from the archive.
  • Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the “Advanced” tab, and the “Environment Variables” button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-2.2.1. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven < 2.0.9, also be sure that the M2_HOME doesn’t have a ‘\’ as last character.
  • In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
  • Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
  • In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
  • In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
  • Open a new command prompt (Winkey + R then type cmd) and run mvn –version to verify that it is correctly installed.

Unix-based Operating Systems (Linux, Solaris and Mac OS X)

  • Extract the distribution archive, i.e. apache-maven-2.2.1-bin.tar.gz to the directory you wish to install Maven 2.2.1. These instructions assume you chose /usr/local/apache-maven. The subdirectory apache-maven-2.2.1 will be created from the archive.
  • In a command terminal, add the M2_HOME environment variable, e.g. export M2_HOME=/usr/local/apache-maven/apache-maven-2.2.1.
  • Add the M2 environment variable, e.g. export M2=$M2_HOME/bin.
  • Optional: Add the MAVEN_OPTS environment variable to specify JVM properties, e.g. export MAVEN_OPTS=”-Xms256m -Xmx512m”. This environment variable can be used to supply extra options to Maven.
  • Add M2 environment variable to your path, e.g. export PATH=$M2:$PATH.
  • Make sure that JAVA_HOME is set to the location of your JDK, e.g. export JAVA_HOME=/usr/java/jdk1.5.0_02 and that $JAVA_HOME/bin is in your PATH environment variable.
  • Run mvn –version to verify that it is correctly installed.

 

Optional configuration

Maven will work for most tasks with the above configuration, however if you have any environmental specific configuration outside of individual projects then you will need to configure settings. The following sections refer to what is available.

Settings

Maven has settings file located in the Maven installation and/or user home directory that configure environmental specifics such as:

  • HTTP proxy server
  • repository manager location
  • server authentication and passwords
  • other configuration properties

Settings Reference

The settings element in the settings.xml file contains elements used to define values which configure Maven execution in various ways, like the pom.xml, but should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information. There are two locations where a settings.xml file may live:

    • The Maven install: $M2_HOME/conf/settings.xml
    • A user’s install: ${user.home}/.m2/settings.xml

Here is an overview of the top elements under settings:

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd”>

 

localRepository: This value is the path of this build system’s local repository. The default value is ${user.home}/.m2/repository. This element is especially useful for a main build server allowing all logged-in users to build from a common local repository.

interactiveMode: true if Maven should attempt to interact with the user for input, false if not. Defaults to true.

usePluginRegistry: true if Maven should use the ${user.home}/.m2/plugin-registry.xml file to manage plugin versions, defaults to false. Note that for the current version of Maven 2.0, the plugin-registry.xml file should not be depended upon. Consider it dormant for now.

offline: true if this build system should operate in offline mode, defaults to false. This element is useful for build servers which cannot connect to a remote repository, either because of network setup or security reasons.

pluginGroups: This element contains a list of pluginGroup elements, each contains a groupId. The list is searched when a plugin is used and the groupId is not provided in the command line. This list automatically contains org.apache.maven.plugins and org.codehaus.mojo. For example, given the above settings the Maven command line may execute org.mortbay.jetty:jetty-maven-plugin:run with the truncated command:

Servers
The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. However, certain settings such as username and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml.

Mirrors
epositories are declared inside a project, which means that if you have your own custom repositories, those sharing your project easily get the right settings out of the box. However, you may want to use an alternative mirror for a particular repository without changing the project files.

Some reasons to use a mirror are:

There is a synchronized mirror on the internet that is geographically closer and faster
You want to replace a particular repository with your own internal repository which you have greater control over
You want to run maven-proxy to provide a local cache to a mirror and need to use its URL instead

Proxies: This is basically used for proxies’ setup

Profiles
The profile element in the settings.xml is a truncated version of the pom.xml profile element. It consists of the activation, repositories, pluginRepositories and properties elements. The profile elements only include these four elements because they concerns themselves with the build system as a whole (which is the role of the settings.xml file), not about individual project object model settings.

If a profile is active from settings, its values will override any equivalently ID’d profiles in a POM or profiles.xml file.

First Maven project?

To create our first Maven project we are going to use Maven’s archetype mechanism. An archetype is defined as an original pattern or model from which all other things of the same kind are made. In Maven, an archetype is a template of a project which is combined with some user input to produce a working Maven project that has been tailored to the user’s requirements.

On to creating your first project! In order to create the simplest of Maven projects, execute the following from the command line:

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app

Once you have executed this command, you will notice a few things have happened. First, you will notice that a directory named my-app has been created for the new project, and this directory contains a file named pom.xml that should look like this:

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.mycompany.app
my-app
jar 1.0-SNAPSHOT
Maven Quick Start Archetype
http://maven.apache.org

junit
junit
3.8.1
test

pom.xml contains the Project Object Model (POM) for this project. The POM is the basic unit of work in Maven. This is important to remember because Maven is inherently project-centric in that everything revolves around the notion of a project. In short, the POM contains every important piece of information about your project and is essentially one-stop-shopping for finding anything related to your project.

This is a very simple POM but still displays the key elements every POM contains, so let’s walk through each of them to familiarize you with the POM essentials:

project: This is the top-level element in all Maven pom.xml files.
modelVersion: This element indicates what version of the object model this POM is using. The version of the model itself changes very infrequently but it is mandatory in order to ensure stability of use if and when the Maven developers deem it necessary to change the model.

groupId: This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.

artifactId: This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form -. (for example, myapp-1.0.jar).

packaging: This element indicates the package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.). This not only means if the artifact produced is JAR, WAR, or EAR but can also indicate a specific lifecycle to use as part of the build process. (The lifecycle is a topic we will deal with further on in the guide. For now, just keep in mind that the indicated packaging of a project can play a part in customizing the build lifecycle.) The default value for the packaging element is JAR so you do not have to specify this for most projects.

version: This element indicates the version of the artifact generated by the project. Maven goes a long way to help you with version management and you will often see the SNAPSHOT designator in a version, which indicates that a project is in a state of development. We will discuss the use of snapshots and how they work further on in this guide.

name: This element indicates the display name used for the project. This is often used in Maven’s generated documentation.

url: This element indicates where the project’s site can be found. This is often used in Maven’s generated documentation.

description: This element provides a basic description of your project. This is often used in Maven’s generated documentation.

 

Tagged : / / / / / / / / / / / / / /