Groovy:从命令行运行时无法解析类

时间:2018-06-27 08:22:31

标签: shell groovy command-line

我正在尝试从命令行运行Groovy脚本。它在intellij的gradle项目中运行良好,但是当我尝试通过命令行运行它时,却遇到了类似这样的错误

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup 
failed:
/home/tpulayan/jenkins- 
watcher/src/main/groovy/attempt_authentification.groovy: 1: unable to resolve 
class in.ashwanthkumar.slack.webhook.Slack
@ line 1, column 1.
  import in.ashwanthkumar.slack.webhook.Slack
  ^

1 error

我的代码:

@Grapes(
    @Grab(group='in.ashwanthkumar', module='slack-java-webhook', 
version='0.0.7')
)

import in.ashwanthkumar.slack.webhook.Slack
import in.ashwanthkumar.slack.webhook.SlackMessage
import org.apache.http.HttpException
import org.apache.http.HttpHost
import org.apache.http.HttpRequest
import org.apache.http.HttpRequestInterceptor
import org.apache.http.auth.AuthScheme
import org.apache.http.auth.AuthScope
import org.apache.http.auth.AuthState
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.CredentialsProvider
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.protocol.ClientContext
import org.apache.http.impl.auth.BasicScheme
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.protocol.BasicHttpContext
import org.apache.http.protocol.ExecutionContext
import org.apache.http.protocol.HttpContext
import org.apache.http.util.EntityUtils


use(TimerMethods) {
    def timer = new Timer()
    def periodSeconds = 600

    new Slack()
            .icon(':exclamation:')
            .sendToChannel('jenkins-monitoring')
            .displayName('Jenkins Watcher')
            .push(new SlackMessage("Jenkins Watcher started at ${new Date()}. Started to monitor the status of deb-jenkins-prd..."))

    def task = timer.runEvery(1000, periodSeconds * 1000) {
        def response = getAuthentication().statusCode
        if (response != 200) {
            new Slack()
                    .icon(':exclamation:')
                    .sendToChannel('jenkins-monitoring')
                    .displayName('Jenkins Watcher')
                    .push(new SlackMessage("Login attempt by Jenkins Watcher failed at ${new Date()}. This could indicate that Jenkins is stuck! Request response code is: ${response}"))
        }
    }
}

我想这与类路径有关,但是我无法解决。

非常感谢您的帮助!

编辑响应后

我已经使用@Grab更新了我的代码,但不幸的是,从命令行运行它时出现了此错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during conversion: Error grabbing Grapes -- [unresolved dependency: in.ashwanthkumar#slack-java-webhook;0.0.7: not found]

java.lang.RuntimeException: Error grabbing Grapes -- [unresolved dependency: in.ashwanthkumar#slack-java-webhook;0.0.7: not found]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
        at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
        at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:238)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:250)
        at groovy.grape.GrapeIvy.getDependencies(GrapeIvy.groovy:464)

1 个答案:

答案 0 :(得分:0)

build.gradle中的

似乎您已定义依赖项。

您可以将所有依赖项(* .jar)放入./lib目录中,并像下面这样运行groovy脚本:

groovy -cp ./lib/* MyScript.groovy

或者您可以直接在Groovy脚本中使用Grape/Grab指定所有依赖项,这样您的命令行将很简单,并且groovy将在脚本启动时下载所有依赖项

groovy MyScript.groovy