java - 使用grab和RESTClient尝试简单的groovy脚本

标签 java groovy

将 Groovy 2.1.7 与 JDK 1.7.0_40 结合使用。

我正在尝试编写一个非常简单的原型(prototype) Groovy 脚本。它将调用 RESTClient 并处理来自服务的结果。

它看起来像这样:

#! env groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' )
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1' )
import groovyx.net.http.RESTClient;
println("About to create RESTClient.");
service = new RESTClient("http://myhost/")
def diagnostics = service.get(path: "mypath");
println("diagnostics[" + diagnostics + "]");

当我运行这个时,我得到这个:

About to create RESTClient.
Caught: java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
        at monitorStatus.run(monitorStatus.groovy:9)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
        ... 1 more

我看到有人提到需要在方法上添加“@Grab”注释。我已经在方法上使用这些注释尝试了几种变体,但没有什么区别。

我在这里做错了什么?

编辑: 我将“-Dgroovy.grape.report.downloads=true”添加到我的 JAVA_OPTS 中,并对脚本进行了一些细微的更改。

这是我当前的脚本,仅修改了 url 和路径:

#! /usr/bin/env groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' )
@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.2.5' )
import groovyx.net.http.RESTClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpUriRequest;
println("About to create RESTClient.");
service = new RESTClient("http:///")
def diagnostics = service.get(path: "mypath");
println("diagnostics[" + diagnostics + "]");

这是我运行时得到的结果:

Resolving dependency: org.apache.httpcomponents#httpclient;4.2.5 {default=[default]}
Resolving dependency: org.codehaus.groovy.modules.http-builder#http-builder;0.5.2 {default=[default]}
Preparing to download artifact org.apache.httpcomponents#httpclient;4.2.5!httpclient.jar
Preparing to download artifact org.codehaus.groovy.modules.http-builder#http-builder;0.5.2!http-builder.jar
Preparing to download artifact org.apache.httpcomponents#httpcore;4.2.4!httpcore.jar
Preparing to download artifact commons-logging#commons-logging;1.1.1!commons-logging.jar
Preparing to download artifact commons-codec#commons-codec;1.6!commons-codec.jar
Preparing to download artifact net.sf.json-lib#json-lib;2.3!json-lib.jar
Preparing to download artifact org.codehaus.groovy#groovy;1.7.11!groovy.jar
Preparing to download artifact net.sourceforge.nekohtml#nekohtml;1.9.9!nekohtml.jar
Preparing to download artifact xml-resolver#xml-resolver;1.2!xml-resolver.jar
Preparing to download artifact commons-beanutils#commons-beanutils;1.8.0!commons-beanutils.jar
Preparing to download artifact commons-collections#commons-collections;3.2.1!commons-collections.jar
Preparing to download artifact commons-lang#commons-lang;2.4!commons-lang.jar
Preparing to download artifact net.sf.ezmorph#ezmorph;1.0.6!ezmorph.jar
Preparing to download artifact antlr#antlr;2.7.7!antlr.jar
Preparing to download artifact asm#asm;3.2!asm.jar
Preparing to download artifact asm#asm-commons;3.2!asm-commons.jar
Preparing to download artifact asm#asm-util;3.2!asm-util.jar
Preparing to download artifact asm#asm-analysis;3.2!asm-analysis.jar
Preparing to download artifact asm#asm-tree;3.2!asm-tree.jar
Preparing to download artifact xerces#xercesImpl;2.8.1!xercesImpl.jar
Preparing to download artifact xml-apis#xml-apis;1.3.03!xml-apis.jar
About to create RESTClient.
Caught: java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpUriRequest
        at monitorSunlightDataSourceStatus.run(monitorSunlightDataSourceStatus.groovy:10)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest
        ... 1 more

这提供了任何有用的线索吗?

最佳答案

尝试删除~/.m2目录以及~/.groovy/grapes目录
它对我有用。

关于java - 使用grab和RESTClient尝试简单的groovy脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18904258/

相关文章:

java - 如何使用 Selenium 注入(inject) XML 文件?

java - HashSet 包含子字符串

java - Primefaces : Select Check Box menu : NullPointerException

groovy - 使用 Groovy Console,是否可以在类路径中重新加载更改的文件?

java - 如何为 JTabbedPane 中的选项卡设置准确的样式?

Java 嵌套包可见性

java - 从数据库发送电子邮件的工具

Grails Controller 操作不能过载

java - 计算一定范围内的频率

Spring-boot JPA 多个数据源不更新或创建表