Skip to main content
AI
  • Get started with AI

    • Red Hat AI

      Accelerate the development and deployment of enterprise AI solutions.

    • AI learning hub

      Explore learning materials and tools, organized by task.

    • AI interactive demos

      Click through scenarios with Red Hat AI, including training LLMs and more.

    • AI/ML learning paths

      Expand your Red Hat OpenShift AI knowledge using these learning resources.

    • AI quickstarts

      Focused AI use cases designed for fast deployment on Red Hat AI platforms.

    • No-cost AI training

      Foundational Red Hat AI training.

  • Featured resources

    • Red Hat OpenShift AI
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

Red Hat AI Factory with NVIDIA

Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.

Learn
  • Self-guided

    • Documentation

      Find answers, get step-by-step guidance, and learn how to use Red Hat products.

    • Learning paths

      Explore curated walkthroughs for common development tasks.

    • Guided learning

      Receive custom learning paths powered by our AI assistant.

    • See all learning
  • Hands-on

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

    • Interactive labs

      Learn by doing in these hands-on, browser-based experiences.

    • Interactive demos

      Click through product features in these guided tours.

  • Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics
  • Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
Build
  • Get started

    • Red Hat build of Podman Desktop

      A downloadable, local development hub to experiment with our products and builds.

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

  • Download products

    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products
  • Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset
  • References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
Community
  • Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions
  • Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github
  • Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

Join the Red Hat Developer program

Download Red Hat products and project builds, access support documentation, learning content, and more.

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

Use Groovy to customize the Maven build process

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

    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

    • A day in the life of an NVIDIA H100 on Red Hat OpenShift 5

    • Migrate BuildConfigs to builds for Red Hat OpenShift with one plug-in install

    • Run Red Hat Enterprise Linux CoreOS on NVIDIA BlueField DPUs

    • Agent harness engineering: Separating model capabilities from enforcement

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    Red Hat Developers logo

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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