Dependency Management - pford68/groovy-examples GitHub Wiki

Groovy Grapes

Grape is a JAR dependency manager embedded into Groovy. Grape lets you quickly add maven repository dependencies to your classpath, making scripting even easier. The simplest use is as simple as adding an annotation to your script:

@Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
  • Built in to Groovy
  • Uses annotations to add a dependency to the classpath quickly.
  • Supports shorthand notation, like Gradle dependencies do.

Specify Additional Repositories

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

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

Excluding Transitive Dependencies

Use @GrabExclude:

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

JDBC Drivers

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

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

Using Grape From the Groovy Shell

From groovysh use the method call variant instead of annotations:

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

References