Auto-Grabbing Dependencies with Groovy Scripts
Now here’s something cool for your once-in-while, small, groovy sysadmin scripts…
When you write a groovy script, chances are that, when it gets just a little bigger than damn-small, it will require some third party dependencies, to be able to run properly. So, what do you do? You end up either a) package dependent jars in a lib folder and dist as a zip with a script that can build a classpath or, b) re-package all dependencies into one big runnable jar including both dependencies and your not so little script anymore.
But now, there’s “Grab”
@Grab(group='com.ancientprogramming.fixedformat4j', module='fixedformat4j', version='1.2.2')
class Grabber {
Grabber() {
println "Code that is supposed to use fixedformat4j"
}
}
new Grabber()
The “Grabber” class above is an example of a class, which is pretending it needs fixedformat4j in its classpath. When compiled, the groovy compiler will add code to lookup and download the dependencies from some repository (maven, ivy). When run, this generated code is executed, dependencies located and downloaded (if needed), and your script is then executed with access to the downloaded classes/jars.
After trying it out, a little digging around made me discover, that the downloaded dependencies end up in $HOME/.groovy/grapes
Cool?
May 18, 2009
Posted in: Uncategorized

Leave a Reply