Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Use Groovy to customize the Maven build process

October 10, 2018
Andrea Tarocchi
Related topics:
Java
Related products:
Red Hat Fuse

Share:

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

    Syndesis dashboard
    Syndesis dashboard

    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.

    Syndesis integration editor
    Syndesis integration editor

    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 highlights crucial 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.

    Last updated: October 9, 2018

    Recent Posts

    • Create and enrich ServiceNow ITSM tickets with Ansible Automation Platform

    • Expand Model-as-a-Service for secure enterprise AI

    • OpenShift LACP bonding performance expectations

    • Build container images in CI/CD with Tekton and Buildpacks

    • How to deploy OpenShift AI & Service Mesh 3 on one cluster

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue