I am learning groovy for a scripting package called geoscript-groovy. I followed the groovy REST tutorial here and tested the following code:
import groovyx.net.http.RESTClient
def client = new RESTClient( 'http://www.acme.com/' )
def resp = client.get( path : 'products/3322' ) // ACME boomerang
However, I got an error at the import
statement saying:
Groovy:unable to resolve class groovyx.net.http.RESTClient
I searched around, and there are many questions and answers for this error message, e.g., import groovyx.net.http.RESTClient in Groovy class , and RestClient Grails Import fails. However, they all for grails, which I don't use and am not very familiar with.
My question is
How should I fix this error if I have groovy only? (My version of groovy is installed under Ubuntu 12.04 with the following commands).
sudo apt-add-repository ppa:groovy-dev/groovy
sudo apt-get update
sudo apt-get install groovy
Thanks.
-- EDIT ---
I added @Grab statements as suggested, and put up a two-line rest1.groovy
file as follows:
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
import groovyx.net.http.RESTClient
groovyConsole rest1.groovy
seems to run OK. But groovysh < rest1.groovy
is still giving me an error (as shown below). I guess I need to have this run in a groovysh
-like environment because the groovy script is called in the background as a web service. Without the @Grab
line, the service generates an Exception. With the @Grab
line, the service won't even register. Is there a more permanent way of including necessary dependencies for groovyx.net.http.RESTClient
than a per script grab (e.g. an apt-get
or manual copying of something)?
groovysh < rest1.groovy
Groovy Shell (1.8.6, JVM: 1.7.0_72)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------
groovy:000> @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
groovy:001> import groovyx.net.http.RESTClient
ERROR org.codehaus.groovy.tools.shell.CommandException:
Invalid import definition: 'import groovyx.net.http.RESTClient'; reason: startup failed:
script1413902882282760571375.groovy: 1: unable to resolve class groovyx.net.http.RESTClient
@ line 1, column 1.
import groovyx.net.http.RESTClient