This post was originally published on Red Hat Developers, the community to learn, code, and share faster. To read the original post, click here.
Apache Maven is a popular build automation tool used primarily for Java projects (although it can also be used to build and manage projects written in other languages). Maven uses a pom.xml
file to centrally manage a project’s build and its dependencies. If you have worked anywhere near to the Java ecosystem chances are that, for the good or for the bad, you have come across the use of this tool.
Maven plugins are used to enhance and customize the Maven build process; while the list of existing plugins is quite extensive, it is common to need to implement some small changes or tweak the build just a bit, which makes writing a whole plugin feel like overkill.
This post describes a possible solution: the GMaven Plus plugin.
GMaven Plus is a Maven plugin for managing Groovy projects with Maven. Among other features, it has an execute script goal that can be configured easily, as shown in the following snippet:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6.1</version>
<executions>
<execution>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<configuration>
<properties>
<property>
<name>someProp</name>
<value>${someProp}</value>
</property>
</properties>
<scripts>
//your Groovy code goes here
</scripts>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.5.0</version>
<type>pom</type>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
The execution of the Groovy script can be bound to any Maven phase; furthermore, inside the script, the objects session
and project
can be used to interact with the Maven build. For instance, the project name can be logged with log.info "$project.name"
. (There are other useful Maven objects available to be used in the Groovy script along with other useful goodies like using @Grab
to download external dependencies your script might have; check the examples). Due to the combination of these two capabilities, it is quite easy to extend the Maven build process directly with an inline Groovy script placed directly in the pom.xml
file, as shown above.
A real-world example
Instead of using a fictional example to showcase how to use the GMaven Plus plugin for our purposes, I think it would be beneficial to look at a real-world example. The example is taken from the [Syndesis](https://syndesis.io/" target="_blank" rel=“noopener) project.
Syndesis is an integration Platform-as-a-Service (iPaaS) that aims to simplify collaboration between business users, integration experts, and application developers. It is a cloud-native toolchain and runtime, available right from your browser. It is an open source project on which Red Hat Fuse Online is based.
Using just the Syndesis web UI, it is possible to define, run, and manage an integration that, for instance, takes data from Twitter and saves it in a database or sends a message to a messaging system like Apache Kafka or Apache ActiveMQ. These tasks historically required an integration specialist and a developer to work together, sometimes for days, but with Syndesis these tasks can be accomplished in a no-code fashion directly by a business user.
The platform is extensible through extensions to cover less-standard and more-particular cases (some extensions are collected in their own GitHub repository). Usually, it is a developer that takes care of creating such an extension and building it. In order to do so, the extensions must be built against the version of the Syndesis platform on which they will be loaded. Manually retrieving and setting the Syndesis version in the syndesis-extension project is a tedious and error-prone activity. It would be much better if you could pass the Syndesis platform URL to the build process and Maven would automatically get the right version and use it.
Solution explained
The Groovy script in syndesis-extension project pom.xml implements the following logic: the URL is passed in a property named syndesisServerUrl
that is used to dynamically retrieve the Syndesis version and set it as syndesis.version
in the Maven project. If no URL is provided, Maven will fall back to using the syndesis.version
present in the pom.xml
file.
The relevant part of the pom.xml
file is shown below:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>get-syndesis-version</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<configuration>
<scripts>
<script>
[...]
log.debug "START getting syndesis version from a running Syndesis server."
def syndesisServerUrl = getPropertyValue('syndesisServerUrl')
log.debug "syndesisServerUrl value = $syndesisServerUrl"
if( syndesisServerUrl != null ) {
[...]
String syndesisVersionUrl = syndesisServerUrl+"/api/v1/version"
log.info "About to call GET on $syndesisVersionUrl"
def version = null
try {
version = new URL(syndesisVersionUrl).getText(requestProperties: [Accept: 'text/plain'])
String syndesisVesrion = new String(version)
project.properties.setProperty('syndesis.version', syndesisVesrion)
log.info "syndesis.version set to: $syndesisVesrion"
} catch(Exception ex) {
log.error "Error during syndesis version GET from $syndesisVersionUrl"
ex.printStackTrace()
throw ex
} finally {
log.info "Called GET on $syndesisVersionUrl with result $version"
}
} else {
log.info "syndesisServerUrl property not set, the syndesis.version from pom.xml will be used."
}
[...]
String getPropertyValue(String name) {
def value = session.userProperties[name]
if (value != null) return value //property was defined from command line e.g.: -DpropertyName=value
return project.properties[name]
}
log.debug "END getting syndesis version from a running Syndesis server."
</script>
</scripts>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.12</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
The above snippet contains the most significant lines. First off, the line
<phase>initialize</phase>
binds the script execution to the initialization phase; hence, it will be executed at the beginning of each Maven build. The rest of the script is a self-explanatory Groovy code implementation of the logic described at the beginning of this section. Perhaps the most crucial line is
project.properties.setProperty('syndesis.version', syndesisVersion)
which sets the syndesis.version
property in the Maven project
object. In this way, if a user invokes the build with
mvn clean install -DsyndesisServerUrl=https://your.syndesis.server.url
the Syndesis version is dynamically taken from the provided URL.
Conclusion
This article presented a way to use the GMaven Plus plugin to tap into a Maven build and interact with it without the need to write a whole Maven plugin. Furthermore, it presented and commented on a real-world usage of that technique in order to provide some context to the discussion.