Methods & Tools Software Development Magazine

Software Development Magazine - Project Management, Programming, Software Testing, Jobs

Maven - Open Source Build Tool for Java

Evgeny Goldin,

Maven is an open source build tool traditionally used in Java and Java EE projects to compile source files, execute unit tests and assemble distribution artifacts. While Maven specializes in Java projects and artifacts, such as .ear and .war applications, it is not limited to those environments and can be equally used for Groovy and Scala projects, which seem to be popular alternatives to Java these days.

Web Site:
Version Tested: 2.2.1, 3.0.3 on Windows 7 / Server 2008 / Ubuntu 10.04, Java 1.6.0_25 x86
License & Pricing: Open Source (Apache license), Free



Installing Maven is simple; all you need to do is download the corresponding archive of Maven 3 from, unpack it and add the "bin" folder to your system PATH. Maven is a multi-platform tool. It runs on every operation system on which Java is installed. Please make sure that you have the JAVA_HOME environment variable defined properly and that you can execute the "java -version" command.

After adding Mavenís "bin" folder to the PATH environment variable, running "mvn Ėversion" will make sure the installation is successful:

Two additional system properties can now be defined: M2_HOME specifies Mavenís installation folder and MAVEN_OPTS specifies JVM options that will be used for every build execution; it can be set to "-Xmx512m -XX:MaxPermSize=256m" if you plan to run large builds which take a long time to run.

You can find further installation details in Chapter 2 of "Maven: The Complete Reference", located at and an online manual at

The POM file

Maven operates on "pom.xml" files, commonly referred as POMs for "Project Object Model". Those XML files declaratively represent your project, its structure and any additional configurations or dependencies applied to the build lifecycle.

A sample POM file is provided below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns = >
	xmlns:xsi =
	xsi:schemaLocation = "">

This POM file provides a description of a web project which is compiled, tested and assembled into a .war archive when "mvn clean install" is invoked. Thereís no need to specify any of those build steps since Maven relies heavily on standards and conventions. As long as your project structure follows these conventions, very little needs to be added. This POM file also shows how we can declaratively specify project dependencies or any other project-specific settings. Relying on conventions and declarative definitions is what makes Maven different from older build tools such as Ant, in which all lifecycle steps need to be specified and managed explicitly. Detailed POM references are available online at: and

Lifecycle, Usage and Plugins

For every module built, Maven activates a build lifecycle composed of phases which build engineers are very familiar with: "clean", "compile", "test", "package", and "install". Many other fine-grained build phases are also available. The full list can be found at and

To execute a Maven build you need to specify phases to be invoked by typing "mvn <phase>", such as "mvn clean install" or "mvn test". When a phase like "install" is specified, all phases preceding it in the lifecycle are also implicitly executed. That includes the "compile", "test" and "package" steps. Note that "clean" is not part of a standard lifecycle and needs to be run explicitly.

Each phase execution is handled by one of the corresponding Maven plugins. Maven itself deals with reading and analyzing POM files, dependencies resolutions and lifecycle management but all actual tasks of compiling the sources, running unit tests and assembling the distribution archive are delegated to one of its core plugins. Their list is provided at and each plugin can be configured separately to match the project requirements. By configuring a plugin you can change any of its default settings, for example specifying additional compilation flags or testing options. You can also attach Mavenís plugin execution to one of the lifecycle phases. This allows changing the behavior of core plugins that are always invoked as part of the standard lifecycle phases, such as "maven-compiler-plugin", but it is also possible to add any other Maven plugin to the build lifecycle.

One sample configuration of a compiler plugin is provided below:


This configuration targets Java sources compiled to run on JDK 1.6. In addition, it enables all compiler warnings and deprecation notifications for developers to notice possible code issues as early as possible.

Maven and Groovy

Even though the list of Maven plugins is an impressive one, you may need more plugins at times. Some builds may have additional requirements not covered by existing plugins. In this case there are a number of ways in which Maven can be extended:

  • By executing Ant code as part of the build.
  • By executing Groovy code as part of the build.
  • By executing custom Maven plugins, developed for specific needs.

From these 3 options, executing custom Groovy code can be a good option to try first since developing Maven plugins may require time and resources not readily available while adding Ant code makes POM files significantly harder to read.

For running Groovy code as part of a Maven build you may use GMaven plugin, as shown below:

			println "Build time is [${ new Date()}]"

This Groovy code snippet is executed after the build artifact is created, in the "package" lifecycle phase. Groovy can perform any additional build actions, interact with POM data and the artifacts created or invoke any other third-party libraries you may need. Further information about GMaven plugin is available on its Web site at


Maven is a very popular and stable build tool, widely used by many developers and organizations these days. It provides a standard build lifecycle, many sensible conventions regarding project organization and declarative ways to define any project-specific settings.

Maven provides a great number of core plugins which handle all traditional build tasks like compiling sources, running unit tests and archiving distribution assemblies. If you still find yourself missing some functionality, then extending Maven with Groovy, Ant or custom plugins is also possible.

Sonatype is the company standing behind Maven and it provides valuable Maven information, free e-books, support, and training. Further information is available online at

More Maven and Java Content

Click here to view the complete list of tools reviews

This article was originally published in the Summer 2011 issue of Methods & Tools