簡體   English   中英

Groovy:從命令行運行時無法解析類

[英]Groovy: unable to resolve class when running from 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)

build.gradle您似乎已經定義了依賴項。

您可以將所有依賴項(* .jar)放入./lib目錄中,並像下面這樣運行groovy腳本:

groovy -cp ./lib/* MyScript.groovy

或者您可以直接在groovy腳本中使用Grape/Grab指定所有依賴項,那么您的命令行將很簡單,並且groovy將在腳本啟動時下載所有依賴項

groovy MyScript.groovy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM