Methods & Tools Software Development Magazine

Software Development Magazine - Project Management, Programming, Software Testing

Scrum Expert - Articles, tools, videos, news and other resources on Agile, Scrum and Kanban

Maven - Open Source Build Tool for Java and Java EE

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: http://maven.apache.org/
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
Documentation:

Support: Mailing list: http://maven.40175.n5.nabble.com/Maven-Users-f40176.html

Installation

Installing Maven is simple; all you need to do is download the corresponding archive of Maven 3 from http://maven.apache.org/download.html, 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 http://www.sonatype.com/books/mvnref-book/reference/installation.html and an online manual at http://maven.apache.org/download.html#Installation.

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 = >http://maven.apache.org/POM/4.0.0
	xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
	xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
		http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.company</groupId>
	<artifactId>web-app</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>

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: http://maven.apache.org/pom.html and http://www.sonatype.com/books/mvnref-book/reference/pom-relationships.html.

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 http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html and http://www.sonatype.com/books/mvnref-book/reference/lifecycle.html.

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 http://maven.apache.org/plugins/ 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:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>2.3.2</version>
	<configuration>
		<source>1.6</source>
		<target>1.6</target>
		<encoding>UTF-8</encoding>
		<compilerArgument>-Xlint:all</compilerArgument>
		<showDeprecation>true</showDeprecation>
		<showWarnings>true</showWarnings>
	</configuration>
</plugin>

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:

<plugin>
	<groupId>org.codehaus.gmaven</groupId>
	<artifactId>gmaven-plugin</artifactId>
	<version>1.3</version>
	<executions>
		<execution>
		<id>run-groovy</id>
		<goals><goal>execute</goal></goals>
		<phase>package</phase>
			<configuration>
			<providerSelection>1.7</providerSelection>
			<source>
			println "Build time is [${ new Date()}]"
			</source>
			</configuration>
		</execution>
	</executions>
</plugin>

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 http://docs.codehaus.org/display/GMAVEN/.

Summary

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 http://www.sonatype.com/.


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

Methods & Tools
is supported by


Testmatick.com

Software Testing
Magazine


The Scrum Expert