Skip to content

Instantly share code, notes, and snippets.

@hgbrown
Created December 29, 2013 16:39
Show Gist options
  • Save hgbrown/8172124 to your computer and use it in GitHub Desktop.
Save hgbrown/8172124 to your computer and use it in GitHub Desktop.
GROOVY:Groovy Grape Example with documentation
#!/usr/bin/env groovy
/**
* Demonstrates how to use Grape to grab dependencies for
* Groovy scripts.
*
* Grape stands for the Groovy Adaptable (Advanced) Packaging Engine, and it is a part of the Groovy installation.
* Grape helps you download and cache external dependencies from within your script with a set of simple annotations.
*
* If, in your script, you require an external dependency, that you know is available in a public repository as Maven Central Repository,
* you can use @Grab annotation to annotate your import, class, or method with a reference to that library, and Groovy will automatically
* download it, cache it, and put it on the class path of your script.
*
* The first time the script gets executed, we experience a delay while the dependencies are downloaded. The second time, the script
* will execute faster because all libraries will be cached in Grape's cache directory, located in .groovy\grapes, in the user's
* home folder. Groovy also provides a command-line tool, grape, to overview and control Grape's library cache.
*
* For instance, to see the modules in the cache simply execute the following command:
*
* grape list
*
* By default, Grape is using the Maven Central Repository (located at http://search.maven.org/) for downloading libraries.
* If you need to add your own repository, then you can either change grapeConfig.xml, which is in fact, an Apache Ivy configuration,
* or you can use the @GrabResolver annotation inside the script itself to make it more portable.
*
* @GrabResolver(name='codehaus', root='http://repository.codehaus.org/')
* class Searcher { ... }
*
* Like with any dependency management tool, sometimes you need to exclude certain dependencies from a dependency tree,
* and there, the @GrabExclude annotation comes to the rescue:
*
* @GrabExclude(group='commons-codec',module='commons-codec')
* class Searcher { ... }
*
* Under the hood, Grape is using the Apache Ivy library for dependency management and resolution.
* If you don't want to wait for the artifact download upon script start, you can use the grape
* install command to install artifacts before execution:
*
* grape install org.apache.httpcomponents httpclient 4.2.1
*
* You can also use the Grape API directly and prefetch the required dependencies using another Groovy script:
*
* import groovy.grape.Grape
* Grape.grab(group: 'org.apache.httpcomponents',module: 'httpclient',version: '4.2.1')
*
* See also:
* - Grape documentation: http://groovy.codehaus.org/Grape
*
*/
@Grab('org.apache.httpcomponents:httpclient:4.2.1')
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpGet
def httpClient = new DefaultHttpClient()
def url = 'http://www.google.com/search?q=Groovy'
def httpGet = new HttpGet(url)
def httpResponse = httpClient.execute(httpGet)
new File('result.html').text =httpResponse.entity.content.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment