- Groovy 2 Cookbook
- Andrey Adamovich Luciano Fiandesio
- 654字
- 2021-07-23 15:57:23
Integrating Groovy into the build process using Maven
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.
Getting ready
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.
How to do it...
As we already mentioned, Maven uses POM. The POM definition is stored in pom.xml
.
- Let's start with the following partial
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.
- Then we can add the dependencies needed to inject Groovy functionality into the Maven build. Just replace
<!-- 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>
- The last step of the configuration is to add the extensions to build goals to let Maven compile the Groovy code. Replace the
<!-- 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>
- Then, in order to compile and test the project, we just use the standard Maven command line:
mvn clean test
- The output of the command should look similar to the following screenshot:
How it works...
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.
There's more...
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.
See also
- Maven homepage: http://maven.apache.org/
- GMaven homepage: http://docs.codehaus.org/display/GMAVEN/Home
- Maven's Groovy Eclipse Compiler plugin documentation: http://groovy.codehaus.org/Groovy-Eclipse+compiler+plugin+for+Maven
- DBA攻堅指南:左手Oracle,右手MySQL
- Learning NServiceBus(Second Edition)
- 精通Nginx(第2版)
- Interactive Data Visualization with Python
- 三維圖形化C++趣味編程
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- C++程序設計基礎教程
- Android Native Development Kit Cookbook
- Java程序設計
- Flutter跨平臺開發入門與實戰
- 案例式C語言程序設計實驗指導
- Arduino可穿戴設備開發
- uni-app跨平臺開發與應用從入門到實踐
- Python函數式編程(第2版)
- Go語言從入門到精通