10

Apologies for the newbie question, but how do you install HTTPBuilder for Groovy?

I've added the http-builder-0.7.jar, http-builder-0.7-source.jar, and http-builder-0.7-javadoc.jar to GROOVY_HOME/lib.

Is there anything else I need to do? The HTTPBuilder website isn't clear.

Code run from GroovyConsole:

import groovy.grape.Grape

Grape.grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )

def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')

I get this in response:

groovy.lang.MissingMethodException: No signature of method: static groovy.grape.Grape.grab() is applicable for argument types: (java.lang.String, java.lang.String, java.lang.String) values: [org.codehaus.groovy.modules.http-builder, http-builder, 0.7]
Possible solutions: grab(java.lang.String), grep(), grab(java.util.Map), grab(java.util.Map, [Ljava.util.Map;), wait(), dump()

EDIT 2:

 @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )

 def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')

Response:

java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase

at ConsoleScript6.run(ConsoleScript6:4)

Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpRequestBase

... 1 more
James.Wyst
  • 859
  • 3
  • 8
  • 13
  • Don't copy it to the lib folder. It makes anything you do almost impossible to reproduce. Why not use a proper build tool like gradle, and use the builder as a dependency? Or use a grab annotation to fetch it if you're just writing a script – tim_yates Mar 05 '15 at 21:45
  • 1
    @tim_yates I removed them from the lib folder. I've been trying to use Grape from GroovyConsole. I'll edit to show you what I did. I attempted to run Grape.bat on my machine but it just closes out. – James.Wyst Mar 05 '15 at 21:53
  • Have you tried getting rid of your import and replacing the grab line with `@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )` – tim_yates Mar 05 '15 at 22:05
  • 1
    Or (even shorter) `@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')` – tim_yates Mar 05 '15 at 22:06
  • @tim_yates I get another exception thrown. I'm editing the post to show you. – James.Wyst Mar 05 '15 at 22:08
  • Hmmmm... Try the [latest](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.codehaus.groovy.modules.http-builder%22%20AND%20a%3A%22http-builder%22): `'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'` – tim_yates Mar 05 '15 at 22:12
  • @tim_yates Just tried it. Same exception thrown. – James.Wyst Mar 05 '15 at 22:14
  • Odd, afaics that should work... – tim_yates Mar 05 '15 at 22:16
  • You need to install the dependencies of HTTPBuilder itself as well - e.g. apache classes. – Opal Mar 06 '15 at 09:57
  • Had very similar exceptions on ubuntu. Which OS do you use? Do you have ivy in classpath? – asm0dey Sep 20 '15 at 16:10

1 Answers1

7

The following example works for me out of the box:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )

def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')
println http

You need to remove any of the dependency jars you added directly to GROOVY_HOME\lib. Manually adding the jars there could create conflicts and cause these types of errors. Check to see if you have manually added the HttpClient libraries to the lib, remove them as well and try again.

EDIT: When using IntelliJ, I have been able to reproduce this behavior once. I already had a single @Grab annotation added to my Groovy script. When I added a second, it didn't seem to download or import the new library.

First of all, if you add a second @Grab, you need to wrap it in the @Grapes annotation like the following (my first mistake):

@Grapes([
        @Grab(group='org.codehaus.gpars', module='gpars', version='1.2.1'),
        @Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
])

After that, I found my solution here: Intellij IDEA not importing dependencies from @Grab in Groovy project, which explains than when using IntelliJ and you encounter this issue, try placing your cursor next to the @Grapes annotation and selecting Alt+Enter then choose the 'Grab the Artifacts' option.

Community
  • 1
  • 1
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • not for me unfortunatly ... still digging into dirt. Tried to delete local dependencies, define custom Grape conf, tried other versions ... still fail to download (despite of the fact I have in my local Grape repo the Jar !!). Any idea please ? – Alex Jul 11 '16 at 16:19
  • Intellij part is what i was look for!.. And once I have the artifacts resolved, just imported with `import groovyx.net.http.HTTPBuilder` and HTTPBuilder, is available to use as `def http = new HTTPBuilder('http://www.codehaus.org')` – OK999 Nov 12 '16 at 18:59
  • @Alex The Alt-Enter approach will not work if you have any compile errors in the class/script you are trying to 'Grab the Artifacts' for. Temporarily comment out any references to HTTPBuilder or other lines that are in error and try the 'Alt-Enter'-->'Grap the Artifacts' option after. – pczeus Jun 19 '17 at 22:42
  • In 2020 this isn't working well. The [http-builder](https://mvnrepository.com/artifact/org.codehaus.groovy.modules.http-builder/http-builder/0.7.1) hasn't been updated since 2014, and it uses a [JSON library](https://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.4) that haven't been updated since 2010 and isn't actually on Maven central anymore (try downloading the JAR: you'll get a 404). – DavidS Nov 06 '20 at 17:30