Imagine you want to write a Groovy script and this script uses a the 3rd party library. How to do?
There are some solutions, such as:
1) Bootstrapping groovy with bash in the groovy script:
#!/bin/bash //usr/bin/env groovy -cp extra.jar:some-spring-jar.jar:etc.jar -d -Dlog4j.configuration=file:/etc/myapp/log4j.xml "$0" $@; exit $? import org.springframework.class.from.jar //... code goes here... println 'Wicket!'
2) Adding libraries in directory .groovy/lib for any client of your script.
3) Using Grape (my favorite):
Using it basically you don’t need to distribute the dependencies when you distribute your script. It will access Maven Central Repository, grab the library and then put it on the classpath via a really simple syntax:
@Grab(group='commons-lang', module='commons-lang', version='2.4')
Here is my example where I needed to create a rest client using groovyx.net.http.RESTClient without any specific hack or strong dependency environment changes:
@Grab goes to Maven Central by default but it’s possible to change to another remote repo or even look for local one. You can check here for more details.