Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Grape.initGrape() is not used in 1.7+

...

Add a Dependency

Code Block

@Grab(group='org.springframework', module='spring', version='2.5.6')
import org.springframework.jdbc.core.JdbcTemplate

...

Not all dependencies are in maven central. You can add new ones like this:

Code Block

@GrabResolver(name='restlet', root='http://maven.restlet.org/')
@Grab(group='org.restlet', module='org.restlet', version='1.1.6')

...

Some maven dependencies need classifiers in order to be able to resolve. You can fix that like this:

Code Block

@Grab(group='net.sf.json-lib', module='json-lib', version='2.2.3', classifier='jdk15')

...

Sometimes you will want to exclude transitive dependencies as you might be already using a slightly different but compatible version of some artifact. You can do this as follows:

Code Block

@Grab('net.sourceforge.htmlunit:htmlunit:2.8')
@GrabExclude('xml-apis:xml-apis')

...

Because of the way JDBC drivers are loaded, you'll need to configure Grape to attach JDBC driver dependencies to the system class loader. I.e:

Code Block

@GrabConfig(systemClassLoader=true)
@Grab(group='mysql', module='mysql-connector-java', version='5.1.6')

...

From groovysh use the method call variant:

Code Block

groovy.grape.Grape.grab([group:'org.springframework', module:'spring', version:'2.5.6'])

...

If you are behind a firewall and/or need to use Groovy/Grape through a proxy server, you can specify those settings on the command like via the http.proxyHost and http.proxyPort system properties:

Code Block

groovy -Dhttp.proxyHost=yourproxy -Dhttp.proxyPort=8080 yourscript.groovy

Or you can make this system wide by adding these properties to your JAVA_OPTS environment variable:

Code Block

JAVA_OPTS = -Dhttp.proxyHost=yourproxy -Dhttp.proxyPort=8080

...

* Multiple calls to grab in the same context with the same parameters should be idempotent. However, if the same code is called with a different ClassLoader context then resolution may be re-run.

  • grab is disabled by default. Starting calling Grape.initGrape() will enable grab. Any calls to grab before initGrape() is called will be ignored. Hence Grape managed classloading is opt in only. Multiple calls ti Grape.initGrape() after the first successful call are ignored.
  • If the args map passed into the grab call has an attribute noExceptions that evaluates true no exceptions will be thrown.
  • grab requires that a RootLoader or GroovyClassLoader be specified or be in the ClassLoader chain of the calling class. By default failure to have such a ClassLoader available will result in module resolution and an exception being thrown (if initGrape()has been called).
    • The ClassLoader passed in via the classLoader: argument and it's parent classloaders.
    • The ClassLoader of the object passed in as the referenceObject: argument, and it's parent classloaders.
    • The ClassLoader of the class issuing the call to grab

...

If you need to change the directory grape uses for downloading libraries you can specify the grape.root system property to change the default (which is ~/.groovy/grape)

Code Block

groovy -Dgrape.root=/repo/grape yourscript.groovy

...

Grape will download Jetty and its dependencies on first launch of this script, and cache them. We're creating a new Jetty Server on port 8080, then expose Groovy's TemplateServlet at the root of the context — Groovy comes with its own powerful template engine mechanism. We start the server and let it run for a certain duration. Each time someone will hit http://localhost:8080/somepage.gsp, it will display the somepage.gsp template to the user — those template pages should be situated in the same directory as this server script.

See Also:

Using Hibernate with Groovy
http://stackoverflow.com/questions/192432/getting-groovys-grape-going