Introduction
This is a Maven 2 Primer (Maven Home). It’s a result of trying to figure out what Maven is, and how to use it, and not finding any online materials that put everything together in a succinct form.
What is Maven?
Maven is a tool to simplify the management of the build, documentation and reporting phases of Java-based projects using a common, centralized set of information.
The essence of Maven
A simple description of a project is created. If the project is organized in a standard fashion, this is frequently an automated process.
The description at its simplest consists of declarations about the project and its dependencies.
The use of a standard organization for a project then allows Maven to infer many things about the project and provide a number of standard features. For example:
- Compile all classes in a project
- Create a jar file containing all the compiled classes for the project
- Run all the tests associated with a project
What’s different about Maven from xxx?
Convention over configuration. Most of what Maven can do for a project is based on conventions about where to find things – .e.g. uncompiled Java classes – and standard things that can be done with these things – e.g. compile and combine into a jar file.
Since it always gets asked, it’s not Maven or Ant, it’s Maven and Ant. Maven can exploit any Ant build facilities available in a project, and the Ant build facilities can in turn use Maven. Everyone plays nice.
Obtaining and installing Maven
If you’re using the latest generation of any of the popular IDEs – Eclipse, Netbeans, Intellij Idea – then Maven either comes pre-installed or is available as a plugin.
The manual installation process is simple.
- Requirements – Java 5 (1.5.x) or Java 6 (1.6.x)
- Download a Maven binary distribution
- Unpack it into some sensible location – e.g. /usr/local/maven-2.2.1
- Create an environment variable M2_HOME and point it to the installed copy of Maven – i.e. export M2_HOME=/usr/local/maven-2.2.1
- Add $M2_HOME/bin to the PATH setting – e.g. export PATH=${PATH}:${M2_HOME}/bin
Follow many sets of good instructions, starting at the Maven Home Site FAQ.
Creating a Maven project from scratch
(All on one line)
mvn archetype:create -DgroupId=net.olioinfo.experiments -DartifactId=maven-experiment1
-DpackageName=net.olioinfo.experiments
Maven-izing an existing project
This depends on the project type, the environment you’re in and whether someone has been kind enough to do the work already.
For instance, if there’s an existing Grails project, and the Maven – Grails plugin is installed, the command
grails:create-pom
will generate a Maven POM from the existing structure.
There may be third-party tools and IDE plugins that will generate a Maven POM for a given situation.
A simple Maven configuration file
This Maven configuration file was generated by the command given above to create a new project.

Explanation of the basics of a Maven POM file
POM = Project Object Model
| Element |
Description |
| groupId |
Identifies the organization that owns the project, and possibly a grouping of projects within that organization. By convention starts with the reversed domain of the organization – just like Java package naming. |
| artifactId |
A unique identifier for the project. Think of this as being within the scope of the groupId. |
| version |
The particular release or version of a project. By convention, if ‘SNAPSHOT’ is included in the version, this indicates a development version. |
| packaging |
The packaging format this project is delivered in. Typically jar or war |
The combination of these four elements in the order groupId, artifactId, version, packaging together should and must uniquely identify that particular version of that project. This combination is called the ‘Maven coordinates’ of a project. That simply means the unique identifier for the project from Maven’s point of view. Among other things, this ensures that no conflicts arise when components are hosted in a Maven repository.
The other elements of the Maven POM file are fairly obvious. The most significant element is the <dependencies> element. It list all the components that this project depends on. For a further discussion the elements allowed within a POM see any standard reference.
Running Maven
Here’s the delight:
mvn install
will compile, run all the tests, package and install the project in the default local repository (~/.m2/repository ).
The Maven home site has several pages about the steps or ‘lifecycle stages’ of a Maven project.