Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 11 Next »


Grape (The Groovy Adaptable Packaging Engine or Groovy Advanced Packaging Engine) is the infrastructure enabling the grab() calls in Groovy, a set of classes leveraging Ivy to allow for a repository driven module system for Groovy. This allows a developer to write a script with an essentially arbitrary library requirement, and ship just the script. Grape will, at runtime, download as needed and link the named libraries and all dependencies forming a transitive closure when the script is run from existing repositories such as Ibiblio, Codehaus, and


This will likely make Groovy 1.7 rather than 1.6. The principal gating issue is Ivy. Ivy is in Beta1, and already for Beta2 there are breaking API changes. We need to wait for Ivy to at least start it's RC cycle before it is brought into the trunk.

General Requirements

  • There must be a means to cause all calls to the Grape system to become no-ops and rely strictly on the JVM classpath.
  • Grape should support bringing in packages from Maven 2 repositories.
  • There should be some notion of 'endorsed modules' that are shortcutted in for easier use. For example: Scriptom, GroovyWS, SwingXBuilder. These can be hosted as regular Maven2 POMS or some other repository mechanism can be used.
  • There should be command line tools similar to RubyGems, to add, remove, enumerate, and update versions of Grapes stored in the local cache.
  • The implementation should not prevent movement into a container style module system such as integrating OSGi or Java 7 Super Packages. (this requirement should not be construed as an endorsement of either)


Module versioning model.

Grape follows the Ivy conventions for module version identificaiton, with naming change.

  • group - Which module group the module comes from. Translates directly to a Maven groupId or an Ivy Organization. Any group matching /groovy(|\..|x|x\..)/ is reserved and may have special meaning to the groovy endorsed modules.
  • module - The name of the module to load. Translated directly to a Maven artifactId or an Ivy artifact.
  • version - The version of the module to use. Either a literal version ('1.1-RC3') or an Ivy Range ('2.2.1,' meaning 2.2.1 or any grater version).

Code Level Support


One or more groovy.lang.Grab annotations can be added to top level Classes to tell the compiler that this code relies on the specific library. This will have the effect of adding the library to the classloader of the groovy compiler. This annotation is detected and evaluated before any other resolution of classes in the script, so imported classes can be properly resolved by a @Grab annotation.

TODO: should we add an appropriate grab call to the <clinit> section of the class?

TODO: How do we add a grab annotation to straight scripts? The AST seems to indicate that import statemens can be annotation (because the ImportNode extends AnnotatedNode) Should we allow imports to be annotated for this purpose then?

Method call

Typically a call to grab will occur early in the script or in class initialization. This is to insure that the libraries are made available to the ClassLoader before the groovy code relies on the code. A couple of typical calls may appear as follows:

  • grab(Object self, String module), grab(Object self, Map attrs), and grab(Object self, Map attrs, Map... dependencies)}} will be added to the DGM so that references to the backing Grape classes will not be needed. These will be proxies to the main Grape class calls.
  • 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. Grape.initGrape() will enable grab. Any calls to grab before initGrape() is called will be 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 RootClassLoader 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
  • grab(String) is a shortcut for endorsed groovy modules and will be equivilant to {{grab(group:'groovy.endorsed', module:<the module>, version:<the version of groovy being run>)}}

    TODO: all discussion of grab(String} are purely hypothetical as it hasn't been prototyped or the needed infrastructure set up

grab(HashMap) Parameters

  • group - <String> - Which module group the module comes from. Translates directly to a Maven groupId. Any group matching /groovy(|\..|x|x\..)/ is reserved and may have special meaning to the groovy endorsed modules.
  • module - <String> - The name of the module to load. Translated directly to a Maven artifactId.
  • version - <String> and possibly <Range> - The version of the module to use. Either a literal version ('1.1-RC3') or an Ivy Range ('2.2.1,' meaning 2.2.1 or any grater version).
    There are two principal variants of grab, one with a single Map and one with an arguments Map and multiple dependencies map. A call to the single map grab is the same as calling grab with the same map passed in twice, so grab arguments and dependencies can be mixed in the same map, and grab can be called as a single method with named parameters.

There are synonyms for these parameters. Submitting more than one is a runtime exception.

  • group:, groupId:, organisation:, organization:, org:
  • module:, artifactId:, artifact:
  • version:, revision:, {{rev:}}

    Grape may want support simple ranges ('2.2.1'..'*') or '2.2.1'..<'3.0.0' meaning any version greater than 2.2.1 but no 3.x version. But there are implementation problems, trying either of these ranges creates a runtime error having to do with enumerating the list.

Arguments Map arguments
  • classLoader: - <GroovyClassLaoder> or <RootClassLoader> - The ClassLoader to add resolved Jars to
  • refObject: - <Object> - The closest parent ClassLoader for the object's class will betreated as though it were passed in as classLoader:
  • validate: - <boolean>, default false - Should poms or ivy files be validated (true), or should we trust the cache (false).
  • changing: - <boolean>, default false - Whether the artifact can change without it's version designation changing.
  • transitive: - <boolean>, default true - Whether to resolve other dependencies this module has or not.
  • noExceptions: - <boolean>, default false - If ClassLoader resolution or repository querying fails, should we throw an exception (false) or fail silently (true).

Tool level support


GroovyStarter has been enhanced with an additonal code--grab{{code}} option. All three arguments are required (group, module, version). Resolved jars will be added to the RootLoader. These modules will be resolved prior to the main class call.

LoaderConfiguration conf files

LoaderConfiguration has been enhanced with an additional 'grab' line option, where the option is followed by all three arguments (group, module, version). Resolved jars will be added as though they were specifically mentioned by a 'load' line option. These lines are similary subject to property repalcement as 'load' lines are.

<groovyc> support

The GroovyC ant task has been enhanced with <grab> child elements. The attributes group, module, and version specify the module to grab.
quote TODO: currently this doesn't work when fork='true'quite

File Level View

The downloaded modules will be stored according to Ivy's standard mechanism with a cache root of ~/.groovy/grape

Command Line Tools

This installs the specified groovy module or maven artifact. If a version is specified for maven artifacts that specific version will be installed.

Lists locally installed modules (with their full maven name in the case of groovy modules) and versions.

This returns the file locations of the jars representing the artifcats for the specified module(s) and the respective transitive dependencies.


The motivation for introducing Grape into groovy is to resolve the library and module proliferation problem we are encountering. As a general purpose language Grape is suitable for a lot of wildly variable tasks. Providing groovy ways to do those things can crate 'bloat' because a large number of classes that are


Credit for coming up with the name Grape goes to Jörg Staudemeyer and grab Andres Almiray.

Use of Maven Repositories

Maven is big, there are tons of Java libraries that are already deployed in a well-known infrastructure. Because almost all(question) of these are straight Java they are all usable in Groovy out of the box.

Use of Ivy

  • Ivy is licensed under the ASL2, which is the same license as Groovy
  • Ivy is focused strictly on Repository Management. Maven has a strong Repository Management portion, but also has other aspects not germane to the needs of the Grape system, namely build management and project management. If the goal is to provide a means to drop unneeded code we shouldn't bring in unneeded code.
  • Ivy provides for an extensible repository resolution system. If we decide to roll our own Groovy Repository we have the infrastructure to do it, we can also support other non-maven repository systems if needed.

Backward Compatibility

GroovyStarter does do some existing library management, namely loading every singe jar in ~/.groovy/lib into the RootLoader. With Grape installed such behavior can continue, and is merely duplicate effort with no adverse implications. In the 2.0 timeframe we may want to look into deprecating or removing the auto-include of the ~/groovy/lib directory.

Reference Implementaion

A 0.0.1 StrawMan verison is available. Command line is not according to current spec. Only list and install <group> <module> <verison> are supported. In addition, 'resolve' is an additional option that will show te local jar files for a particular set of (<g> <m> <v>)+.
A 0.0.2 Proof of concept version is available. This shows the grab method, the @Grab annotation, and command line tools. Examples are available in subversion in src/examples, except for the annotation, where the bese examples are in the test class. This version may conflict with Gant, only one of the Ivy files should exist under $GROOVY_HOME/libs. If the beta-1 jar is left in place then Grape may not work.

Future Directions

We may want to increase the kind of repositories that Grape can handle from simple Ivy and Maven repositories to other areas such as OSGi Repository Bundles , JNLP (WebStart) files and JSR-277 JAM files.

  • No labels