Groovy+Grapes Make Great Scripts

I love using Groovy scripts for little utilities programs. I have used it to write a nice REST+JSON functional test framework called Green Tea Test. I have also used it to write a simple database schema migration tools called Porpoise.

Scripts are great for passing to another developer. However, if the script has a dependent library, it can often times be difficult to simply run the script as-is. That is where Groovy’s Grape dependency downloader works wonderful. By simply adding a few lines at the top of a groovy script will download the specific version of all the required libraries.

You can use the MVN Repository site to look up the versions available. Once you find the library version you require, simply click on the “Grapes” tab to get the syntax.
4

If you require more than one library, you will need to surround the @Grapes parameters in square brackets.

1@Grapes([
2        @Grab(group='com.h2database', module='h2', version='1.3.170'),
3        @Grab(group='net.sourceforge.jtds', module='jtds', version='1.2.4')
4])

There is one final thing I found out while writing Porpoise. There are rare occasions where you will need the jar available to the system classloader. One such example is using JDBC drivers. In these cases, you should use a GrabConfig to indicate the system class loader should be used.

1@GrabConfig(systemClassLoader=true)

The past several years has seen Groovy grow up and be more accepted by developers. I feel it is a great tool to add to your knowledge, especially if most of your work in on the JVM. Drop a line in the comments and let me know how you have used it.

comments powered by Disqus