Installing HTTPBuilder for Groovy

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-11 01:29:02

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/28888337/installing-httpbuilder-for-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!