Apache Groovy’s Grape for easy dependencies
If you’re tired of manually downloading libraries for your Apache Groovy projects, meet Grape: Groovy’s built-in tool for easily managing dependencies. It lets you declare libraries directly in your code and Grape handles the download and configuration automatically. Even though the official Groovy documentation is thorough, this article provides a quick overview with added context – especially for those familiar with Java libraries. (If you haven’t installed Groovy yet, please read the intro to this series.)
Java comes with a very rich set of libraries that provide an enormous amount of capacity beyond the basic language, and Groovy programmers get to access those libraries due to Groovy’s compatibility with Java. Moreover, the designers of Groovy have enhanced the standard Java libraries in ways that make them, well, groovier. By way of a simple example, Groovy enhances the Object class by adding a sprintf() method, providing the same functionality as the Java String.format() method; but sprintf() is just more compact and also familiar to C programmers. Plus, since it’s added to the top of the class hierarchy, it’s available everywhere.
But no matter how rich the Java (or Groovy) standard libraries are, many things don’t belong in standard libraries – for example, very specialized functionality; or alternative approaches to the way a task is handled by the standard library.
A good example of a library that contains both specialized functionality and alternative approaches is the Apache Commons, a collection of contributed code. Some components incorporated in Apache Commons are Collections, which extends and enhances Java Collections; Math, which provides a broad range of mathematical and statistical functions; and Text, which provides an equally broad range of string manipulation functionality.
Apart from Apache Commons, many other organizations are making and distributing various Java libraries to solve various interesting problems.
The good news is there’s a central repository called Maven Central, that stores a vast collection of Java libraries. This repository is specifically designed to work seamlessly with Apache Maven, a popular tool that manages software components, including builds and documentation, for Java projects.
Let’s say we’re interested in getting the latest version of Apache Commons CSV, which is a library for handling CSV (comma-separated value) format text files. We can search for it on Maven Central, and we’ll be sent to a page that shows all the versions available on Maven Central. As of when I’m writing this, the latest version shown on org.apache.commons is 1.1.0 of 2023-01-28. If I click on the link for that version, I am taken to a page that offers various items, including one titled “Groovy Grape”, which shows this:
@Grapes(
@Grab(group='org.apache.commons', module='commons-csv', version='1.10.0')
)
If I want to get this version of Apache Commons CSV for use with my Groovy script, I just preface the script with the @Grab line above, followed by import statements needed to bring in the desired classes from the library.
So in principle, Grape is to Groovy as pip is to Python; a streamlined way to include useful libraries without doing a bunch of manual download of .jar files.
Beyond dependency management, Grape offers an unexpected benefit: built-in documentation. It automatically records the specific versions of libraries used in your Groovy project. This information becomes invaluable when revisiting old code, especially code that hasn’t been run in a while or on a different system. With Grape’s documentation, you can easily identify and reinstall the necessary libraries to get your project running again quickly and efficiently.
There’s more to come in the Apache Groovy series, catch up on the other tutorials in the meantime.
