Easy Groovy Script Deployment with Grape
You can use groovy for lots of stuff, but many of us use it for simple, one-off administration tasks like upgrading a database schema, doing some reporting or other small tasks. Given the absolute great integration of groovy into the Java platform, a groovy script quickly ends up utilizing a bunch of jar dependencies from various open source frameworks. So, how to easy deploy a script, so that it can be started on the target platform with the least fiddling?
Use Grape!
With groovy comes Grape and the @Grab anotation. Grape is capable of downloading dependencies from maven repositories, much like maven does at compile time, but Grape can do it at execution time of the groovy script. In addition to the automatic resolving and downloading of dependencies, Grape is also capable of adding the dependencies on the script classpath before running it. Cool eh!? Here’s a small example:
package net.techper.foo.bar;
import org.jfree.chart.JFreeChart
import org.joda.time.DateMidnight
@Grapes([
@Grab(group = 'jfree', module = 'jfreechart', version = '1.0.12'),
@Grab(group = 'joda-time', module = 'joda-time', version = '1.6'),
])
class YourCoolTool {
....
}
This script use jfreechart and jodatime, both of which will be downloaded at runtime and added to classpath before the actual script execution.
Grape Internal and Config
Of course, dependencies only need to be downloaded the first time.В The downloaded files are kept in $HOME/.groovy/grapes in a maven-like repository structure.
So what about dependencies that are not in the public repository? Well, you can get them too, as long as the target environment, in which the groovy script is executed, can reach the local repository, where you have these non-public dependencies stored.
Internally, Grape is implemented with Ivy. And Grape can be configured with a Ivy configuration file. Inside the jar distribution of standard groovy lies a default Ivy config, which has some of the major maven repositories predefined. You can provide your own though, by adding a file $HOME/.groovy/grapeConfig.xml which could look something like this:
<ivysettings>
<settings defaultResolver="downloadGrapes"/>
<resolvers>
<chain name="downloadGrapes">
<filesystem name="cachedGrapes">
<ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
<artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]"/>
</filesystem>
<!-- todo add 'endorsed groovy extensions' resolver here -->
<ibiblio name="codehaus" root="http://repository.codehaus.org/" m2compatible="true"/>
<ibiblio name="ibiblio" m2compatible="true"/>
<ibiblio name="java.net2" root="http://download.java.net/maven/2/" m2compatible="true"/>
<ibiblio name="yourid" root="http://your/local/maven/repository/" m2compatible="true"/>
</chain>
</resolvers>
</ivysettings>
Notice the extra ibiblio element, that is used to define a private repository!
All this makes it possible to write a small script in groovy, deploy it simply by copying it to the target environment, and then execute it at that place with no extra fuss.
April 18, 2010
Tags: deployment, grab, grape, groovy, ivy Posted in: Programming

2 Responses
Tech Per » Groovy Grape – Adding Dependencies to Root ClassLoader - April 19, 2010
[...] using grape in groovy to download dependencies and add to classpath, they will by default be added to the GroovyClassLoader when the script is run. This can be a [...]
Tech Per » @Grab Support in IntelliJ IDEA - April 21, 2010
[...] I wrote about the great Grape stuff in Groovy for use when deploying groovy [...]
Leave a Reply