Java Reference
In-Depth Information
5.5. Grapes and @Grab
The Grape mechanism allows you to declare library dependencies directly inside a Groovy
script. This is useful when you need to deliver a script to a client that doesn't already have
the required dependencies but is willing to download them as part of the build process.
The overall API is called Grape (Groovy Adaptable/Advanced Packaging Engine) and
starts with the groovy.lang.Grab annotation. It uses an Ivy resolver to identify and
download dependencies. Its primary use case is on scripts, so that they can be delivered
to a client without any setup requirements other than having Groovy installed. At runtime
Groovy will download and install any declared libraries and their transitive dependencies
as part of the execution process.
Grape Use Case
Grape allows you to deliver a simple script that can be executed by a client without any
setup necessary other than installing Groovy, making it particularly convenient for testers
or QA people.
To demonstrate the Grape system, let me choose the Math library from the Apache Com-
mons project ( http://commons.apache.org/math/ ). Specifically, I want to work with the
complex numbers package. The package includes a class called Complex , which repres-
ents complex numbers. Although the class is interesting in itself, it also makes for a nice
demonstration of Groovy's metaprogramming capabilities.
In Maven syntax the library has a group ID of org.apache.commons , an artifact ID of
commons-math3 , and a version of 3.0. Therefore, the format of the @Grab annotation is
as shown in the following script:
import org.apache.commons.math3.complex.*
@Grab('org.apache.commons:commons-math3:3.0')
Complex first = new Complex(1.0, 3.0);
Complex second = new Complex(2.0, 5.0);
Search WWH ::




Custom Search