简体   繁体   English

如何将依赖项添加到 groovy 脚本中?

[英]How add dependencies into groovy script?

I try to add HttpBuilder into groovy script, but can do it only manually ( Alt + Ctrl + Shift + S add dependencie).我尝试将 HttpBuilder 添加到 groovy 脚本中,但只能手动完成( Alt + Ctrl + Shift + S添加依赖项)。 But when I start script I have error in line of creating new httpbuilder instance java.lang.ClassNotFoundException: org.apache.http.client.HttpClient.但是当我启动脚本时,我在创建新的 httpbuilder 实例 java.lang.ClassNotFoundException: org.apache.http.client.HttpClient 时出错。 I manualy add HttpClient, butClassNotFoundException: net.sf.json.JSONObject and so on.我手动添加 HttpClient, butClassNotFoundException: net.sf.json.JSONObject 等等。 But when I add Ini library it works fine.但是当我添加 Ini 库时它工作正常。

I also tried to use @Grab我也尝试使用@Grab

main()
def main() {
    @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')    

And have compilation error Error:Groovyc: While compiling GroovyTests: java.lang.RuntimeException: Error grabbing Grapes -- [download failed: net.sf.json-lib#json-lib;2.3!json-lib.jar]并有编译错误 Error:Groovyc: While compiling GroovyTests: java.lang.RuntimeException: Error Grapes -- [下载失败:net.sf.json-lib#json-lib;2.3!json-lib.jar]

And net in def http = new groovyx.net.http.HTTPBuilder(' http://www.codehaus.org ') is red and Cannot resolve a symbol 'net' error并且 net in def http = new groovyx.net.http.HTTPBuilder(' http://www.codehaus.org ') 是红色的并且无法解析符号 'net' 错误

will be glad to any help很乐意提供任何帮助

Since you have now installed the groovy executables as per the comments, the following code:由于您现在已经根据注释安装了 groovy 可执行文件,因此以下代码:

@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')

import groovyx.net.http.HTTPBuilder

def http = new HTTPBuilder('https://jsonplaceholder.typicode.com')
def res  = http.get(path: '/users')

println "Number of users: ${res.size()}"

should now run and print:现在应该运行并打印:

─➤ groovy solution.groovy
Number of users: 10

─➤ 

(tested on Groovy Version: 2.5.8 JVM: 1.8.0_232 Vendor: AdoptOpenJDK OS: Linux) (在 Groovy 版本:2.5.8 JVM:1.8.0_232 供应商:AdoptOpenJDK 操作系统:Linux 上测试)

One thing that might be disrupting the artifact resolution is if you have a custom grapeConfig.xml file.可能会干扰工件解析的一件事是,如果您有一个自定义的grapeConfig.xml文件。 This file (if it exists) should be under <user home dir>/.groovy/grapeConfig.xml and the default text that groovy uses if no grapeConfig.xml file is present can be found here:这个文件(如果存在)应该在<user home dir>/.groovy/grapeConfig.xml下,如果没有grapeConfig.xml 文件,groovy 使用的默认文本可以在这里找到:

https://github.com/apache/groovy/blob/master/src/resources/groovy/grape/defaultGrapeConfig.xml https://github.com/apache/groovy/blob/master/src/resources/groovy/grape/defaultGrapeConfig.xml

In addition, if you need to debug grapes downloads, you should try the following flags (again as mentioned in the comments):此外,如果您需要调试葡萄下载,您应该尝试以下标志(再次如评论中所述):

─➤ groovy -Dgroovy.grape.report.downloads=true -Divy.message.logger.level=4 yourGroovyScript.groovy

which should print information on what grapes are actually doing when the resolution fails.当解析失败时,它应该打印有关葡萄实际在做什么的信息。

What does your groovy -v look like?你的groovy -v什么样的? ie what version of groovy and what jdk are you on?即您使用的是什么版本的 groovy 和什么 jdk?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM