Apache Maven (http://maven.apache.org/) is a software project build tool that uses the POM (Project Object Model) to describe project artifacts, dependencies, and rules in a declarative form.
Apache Maven was an important milestone in Java build tool evolution. Together with important features such as organized dependency management and declarative build scripts, it also brought a variety of standards and conventions that have been widely adopted by the Java community, even if Maven itself is not used.
In this recipe, we are going to look into how to compile, test, and run a fully blown Groovy project using the Apache Maven build tool.
We are going to re-use the same Groovy project presented in the Integrating Groovy into the build process using Ant recipe. We also assume that you are familiar with Apache Maven and that it is installed on your machine and is ready for use. The steps of this recipe were tested against Maven 3. For those who don't have it yet, download and installation instructions can be found at http://maven.apache.org/download.cgi.
As we already mentioned, Maven uses POM. The POM definition is stored in pom.xml
.
pom.xml
:<?xml version="1.0" encoding="UTF-8"?> <project> <modelVersion>4.0.0</modelVersion> <groupId>org.groovy.cookbook</groupId> <artifactId>build-groovy</artifactId> <version>1.0-SNAPSHOT</version> <!-- dependencies --> <!-- build --> </project>
It's not much different from the standard POM you would use for any Maven project.
<!-- dependencies -->
comment line with the following:<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.codehaus.gmaven.runtime</groupId> <artifactId>gmaven-runtime-2.0</artifactId> <version>1.4</version> </dependency> </dependencies>
<!-- build -->
comment with the following snippet:<build> <plugins> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <version>1.4</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.codehaus.gmaven.runtime</groupId> <artifactId>gmaven-runtime-2.0</artifactId> <version>1.4</version> </dependency> </dependencies> </plugin> </plugins> </build>
mvn clean test
We have chosen to use the GMaven plugin to integrate Groovy and Maven. The gmaven-runtime-2.0
library is part of that plugin, and it contains transitive references to required Groovy distribution libraries including the groovy-all
library.
We instruct GMaven to execute during the standard compile
and testCompile
goals. Unfortunately, due to the nature of the GMaven plugin, we need to list the gmaven-runtime-2.0
library in the plugin dependencies once again.
After command execution, Maven (with the help of GMaven) compiles and tests the Groovy classes and reports the status in the console. Files produced during the build will be placed under the target
directory, which will contain the .class
files and JUnit reports.
If you want to use the latest Groovy release, which may not yet be referenced by gmaven-runtime
, then you can override the groovy-all
dependency in the following way (both for project and plugin configuration):
<dependencies> <dependency> <groupId>org.codehaus.gmaven.runtime</groupId> <artifactId>gmaven-runtime-2.0</artifactId> <version>1.4</version> <exclusions> <exclusion> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.1.6</version> </dependency> </dependencies>
There is also a possibility to use Groovy to supply additional logic to Maven goals. The same GMaven plugin can be used for that. For example, the following snippet executes Groovy script after the compile
phase is finished, and it prints a list of dependencies:
<execution> <id>compile</id> <phase>compile</phase> <goals> <goal>execute</goal> </goals> <configuration> <source> println "Name: $project.name" println 'Dependencies: ' project.dependencies.each { println it.artifactId } </source> </configuration> </execution>
This snippet has to be added inside the GMaven plugin configuration under the <executions>
tag. Also, please note that if you use GMaven for Groovy code compilation, then you need to set unique the <id>
of your <execution>
elements.
The output of the snippet will appear in the build output and will look as follows:
[INFO] --- gmaven-plugin:1.4:execute (compile) @ build-groovy --- Name: build-groovy Dependencies: junit gmaven-runtime-2.0
As an alternative to GMaven, there's also the Eclipse Groovy Compiler plugin for Maven that can be used instead of GMaven. They are very similar in functionality, and it's hard to recommend one over the other.
3.144.28.70