Managing dependencies in groovy scripts


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:

//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 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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s