【发布时间】:2018-04-12 22:12:32
【问题描述】:
我正在尝试在我的简单 Groovy 脚本中使用“HTTPBuilder”。当我使用“@Grab”导入依赖项时,一切正常。不过,我想将 jar 保存在不同的目录中并使用 classLoader 函数将其导入。我已经复制了 '@Grab' 放置到我的grapes 目录中的'http-builder-0.7.jar' 并将其粘贴到我的Groovy 脚本正在运行的同一目录中(在Windows 上)。然后我注释掉“@Grab”语句并包含 classLoader,但得到这个错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: 启动失败:C:\Groovy Scripts\test.groovy: 9: 无法解析 类 HTTPBuilder
任何想法为什么 classLoader 不能在脚本中工作?我在使用“@Grab”导入时打印出 jar 的路径,它肯定使用的是葡萄目录中的路径。如果我取消注释“@Grab”语句,它会再次起作用。这是小脚本...
//@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')
this.getClass().classLoader.rootLoader.addURL(new File("http-builder-0.7.jar").toURL())
//return new File(groovyx.net.http.HTTPBuilder.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
def http = new HTTPBuilder('http://httpbin.org/get')
【问题讨论】:
-
HttpBuilder 可能依赖于其他依赖...这是一个滑坡走下去
-
有没有办法查看它所依赖的依赖项?这会像下载这些 jar 并将它们放在同一个目录中一样简单吗?
-
You can see them in the POM here 问题是,它是一棵依赖树,所以它所依赖的东西又会产生依赖......因此是滑坡。你最好使用 Grab,或者使用 gradle 之类的东西将你的脚本构建到一个胖 jar 中
-
你试过把这个 jar 放在 groovy-root-folder/lib 上吗?