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.

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.

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.

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.

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:

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/.