微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jenkins共享库:通过Builder模式实例化类失败

如何解决Jenkins共享库:通过Builder模式实例化类失败

CloudBees Core:2.249.1.2-滚动

此处的代码驻留在Jenkins共享库中,而该库又被其他Jenkins共享库调用

以下是标准Builder模式的实现,该模式用于创建自定义HttpRequest类的实例。

class HttpRequest {

    //Mandatory
    private final String url
    private final String httpMethod
    private final String requestContentType
    private final String acceptContentType

    //Optional
    private final String requestBody
    private final List<Integer> validResponseCodes

    private HttpRequest(HttpRequestBuilder httpRequestBuilder) {
        //Mandatory
        this.url = httpRequestBuilder.url
        this.httpMethod = httpRequestBuilder.httpMethod
        this.requestContentType = httpRequestBuilder.requestContentType
        this.acceptContentType = httpRequestBuilder.acceptContentType

        //Optional
        this.requestBody = httpRequestBuilder.requestBody
        this.validResponseCodes = httpRequestBuilder.validResponseCodes
    }

    static class HttpRequestBuilder {
        //Mandatory
        private final String url
        private final String httpMethod
        private final String requestContentType
        private final String acceptContentType

        //Optional
        private String requestBody
        private List<Integer> validResponseCodes

        HttpRequestBuilder(String url,String httpMethod,String requestContentType,String acceptContentType) {
            this.url = url
            this.httpMethod = httpMethod
            this.requestContentType = requestContentType
            this.acceptContentType = acceptContentType
        }

        HttpRequest build() {
            return new HttpRequest(this)
        }

        /**
         *
         * @param requestBody A string in a format valid according to the Content-Type.
         * @return
         */
        HttpRequestBuilder requestBody(String requestBody) {
            this.requestBody = requestBody
            return this
        }

        HttpRequestBuilder validResponseCodes(List<Integer> validResponseCodes) {
            this.validResponseCodes = validResponseCodes
            return this
        }
    }

    String getUrl() {
        return url
    }

    String getHttpMethod() {
        return httpMethod
    }

    String getRequestContentType() {
        return requestContentType
    }

    String getAcceptContentType() {
        return acceptContentType
    }

    String getRequestBody() {
        return requestBody
    }

    List<Integer> getValidResponseCodes() {
        return validResponseCodes
    }
}

类中的调用,即。 Change.groovy如下:

HttpRequest httpRequest = new HttpRequest.HttpRequestBuilder(fullURL,HttpConstants.HTTP_METHODS.PUT.name(),HttpConstants.REQUEST_CONTENT_TYPE_JSON,HttpConstants.ACCEPT_TYPE_JSON
).requestBody(JsonOutput.toJson(authorizePayloadMap))
.validResponseCodes(validResponseCodes)
.build()

错误

org.jenkinsci.plugins.workflow.cps.cpsCompilationErrorsException: startup Failed:
file:/var/jenkins_home/jobs/Cores%20Services/jobs/pseudo-id/branches/CORES-1961-retrieve-print-error/builds/5/libs/jenkins-shared-utilities/src/com/Change.groovy: 127: unable to resolve class HttpRequest.HttpRequestBuilder 
 @ line 127,column 35.
           HttpRequest httpRequest = new HttpRequest.HttpRequestBuilder(fullURL,^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.compilationunit.applyToSourceUnits(compilationunit.java:958)
    at org.codehaus.groovy.control.compilationunit.doPhaSEOperation(compilationunit.java:605)
    at org.codehaus.groovy.control.compilationunit.compile(compilationunit.java:554)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:254)
    at groovy.lang.GroovyClassLoader.recompile(GroovyClassLoader.java:761)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:718)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
    at java.lang.classLoader.loadClass(ClassLoader.java:405)
    at org.jenkinsci.plugins.workflow.cps.cpsgroovyshell$TimingLoader.loadClass(cpsgroovyshell.java:170)
    at java.lang.classLoader.loadClass(ClassLoader.java:405)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:775)
    at org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.getValue(UserDefinedGlobalVariable.java:57)
    at org.jenkinsci.plugins.workflow.cps.cpsScript.getProperty(cpsScript.java:135)
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
    at groovy.lang.Closure.getPropertyTryThese(Closure.java:312)
    at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:306)
    at groovy.lang.Closure.getProperty(Closure.java:295)
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
    at groovy.lang.Closure.getPropertyTryThese(Closure.java:312)
    at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:306)
    at groovy.lang.Closure.getProperty(Closure.java:295)
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:174)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:456)
    at com.cloudbees.groovy.cps.sandBox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at openSnapChange.call(openSnapChange.groovy:140)
    at com.cloudbees.groovy.cps.cpsDefaultGroovyMethods.use(cpsDefaultGroovyMethods:407)
    at openSnapChange.call(openSnapChange.groovy:59)
    at withStageNotification.call(withStageNotification.groovy:55)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
    at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
    at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
    at sun.reflect.GeneratedMethodAccessor333.invoke(UnkNown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandBoxContinuable.access$001(SandBoxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandBoxContinuable.run0(SandBoxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.cpsThread.runNextChunk(cpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.cpsThreadGroup.run(cpsThreadGroup.java:400)
    at org.jenkinsci.plugins.workflow.cps.cpsThreadGroup.access$400(cpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.cpsThreadGroup$2.call(cpsThreadGroup.java:312)
    at org.jenkinsci.plugins.workflow.cps.cpsThreadGroup$2.call(cpsThreadGroup.java:276)
    at org.jenkinsci.plugins.workflow.cps.cpsVmExecutorService$2.call(cpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:136)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

我俯瞰什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?