jenkins - Jenkins 管道中的 Artifactory : org. codehaus.groovy.runtime.GStringImpl 无法转换为 java.lang.String

标签 jenkins groovy jenkins-pipeline artifactory

我的 Jenkinsfile 中有这个:

stage('Artifactory Deploy') {
    options {
        timeout(time: 5, unit: 'MINUTES')
    }
    when {
        anyOf {
            branch "master"
            branch "develop"
        }
    }
    steps {
        withMaven(jdk: "${JDK_VERSION}", maven: 'M3') {
            script {
                def server = Artifactory.server('itext-artifactory')
                def rtMaven = Artifactory.newMavenBuild()
                rtMaven.deployer server: server, releaseRepo: 'releases', snapshotRepo: 'snapshot'
                rtMaven.tool = 'M3'
                def buildInfo = rtMaven.run pom: 'pom.xml',
                        goals: "--threads 2C --no-transfer-progress " +
                                "install --activate-profiles artifactory " +
                                "-Dmaven.repo.local=${env.WORKSPACE.replace('\\','/')}/.repository"
                server.publishBuildInfo buildInfo
            }
        }
    }
}

${env.WORKSPACE.replace('\\','/')} 是必需的,因为虽然 Linux 的行为符合预期,但 Windows 的行为显然类似于 T̪̩͑̒̀o̭̦̱͎̗̲̎̿̎ͩ̓̏̔n̊̀ͅy̲͓̘ͥͬ̾ͮ̌ͮ̐ ̘̪̝̳̺ ̖t̪͎̒̓͑̆̎̚h̵ͥͯ̅ͯ̚ş̟̩̞̹̫̌ͦ̉͂ͨ̔ͣͅ ̺͋̆͂̅ͥP̵̖̞͕̲̂̄ͥͩơ̙͇̫̰͍ͮ̽̓nͨ̄ͮyͨ ̘̪ͯ͂ͪ 关于路径分隔符。

我更愿意

withMaven(jdk: "${JDK_VERSION}", maven: 'M3', mavenLocalRepo: '.repository')

但我们不能拥有美好的东西。

无论如何,当在 Jenkins 上运行这个时,我收到以下错误:

java.lang.ClassCastException: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String
    at org.jfrog.hudson.pipeline.common.types.builds.MavenBuild.run(MavenBuild.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:160)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:158)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:162)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.run(WorkflowScript:193)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at sun.reflect.GeneratedMethodAccessor290.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.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
    at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
    at sun.reflect.GeneratedMethodAccessor340.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.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:107)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at sun.reflect.GeneratedMethodAccessor290.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.ContinuationGroup.methodCall(ContinuationGroup.java:89)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at sun.reflect.GeneratedMethodAccessor290.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:131)
    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)
    at java.lang.Thread.run(Thread.java:748)

并且没有任何 Artifactory 部署到 Artifactory(并且构建失败)。

如何解决这个问题?上游错误修复或本地解决方法。

在 Stack Overflow 上发布我的问题,因为我们拥有 Artifactory Pro 许可证,并且 Jfrog 的客户支持门户仅适用于 Pro X、Enterprise 和 Enterprise+ 客户。其他客户会被定向到 Stack Overflow,请参阅 https://jfrog.com/support/ .

最佳答案

可能的根本原因是在 GStringImpl cannot be cast to java.lang.String 的评论中发现的。 :

if you try to assign a GString to something that is statically typed as String (or pass a GString for a String-type method parameter) then Groovy will do the conversion for you, but if you assign to something that is typed as Object then you have to convert it yourself.

因为我在大括号中使用变量 (${env.WORKSPACE.replace('\\','/')}),所以我自动拥有一个 Groovy GString 而不是 Java String

所以,Jfrog 工作人员,如果您正在监视 Stack Overflow 上的 artifactory 标记:根据 Jenkins documentation for the Artifactory plugingoals 被定义为 String 类型,但显然情况并非如此,因为 Groovy 无法进行转换?你能否证实或解释一下?谢谢!

编辑

我找到了Jenkins Artifactory Plugin API Javadocsthe run() method of org.jfrog.hudson.pipeline.common.types.builds.MavenBuild采用 Map 作为参数:

public void run(Map<String,Object> args)

事实上,虽然 Groovy 会自动执行从 Groovy GString 到 Java String 的转换,但它不会执行从 Groovy Gstring 的转换Map 内的 Object

解决方法

如链接问题中所述,我添加了 .toString() 来手动将 Groovy GString 转换为 Java String

所以

def buildInfo = rtMaven.run pom: 'pom.xml',
        goals: "--threads 2C --no-transfer-progress " +
                "install --activate-profiles artifactory " +
                "-Dmaven.repo.local=${env.WORKSPACE.replace('\\','/')}/.repository"

成为了

def buildInfo = rtMaven.run pom: 'pom.xml',
        goals: "--threads 2C --no-transfer-progress " +
                "install --activate-profiles artifactory " +
                "-Dmaven.repo.local=${env.WORKSPACE.replace('\\','/')}/.repository".toString()

关于jenkins - Jenkins 管道中的 Artifactory : org. codehaus.groovy.runtime.GStringImpl 无法转换为 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60304068/

相关文章:

jenkins-pipeline - 当管道名称以交付结束时,如何跳过 Jenkinsfile 管道中的一个阶段?

c++ - 如何配置 Jenkins 以检测 SVN 更改并执行作业?

git - Jenkins 无法获取changeId 和revisionId

grails - Cron如何在Grails中执行Groovy脚本

grails - grails log4j事件记录器

Jenkins 在更改代理后卡住 sh 命令

jenkins - 是否可以暂停一个阶段,直到 Jenkins 声明性管道中的某个时间到达?

Jenkins - 更改主页的标题

SSH 公钥登录突然停止工作(CENTOS 6)

groovy - Groovy 中的最终静态对象