Groovy @Delegate class annotations are not supported in the pipeline shared library jenkins

喜欢而已 提交于 2020-01-16 05:21:25

问题


jenkins ver. 2.89.4
workflow-cps ver 2.42

When i try to run a pipeline that loads the library, i get the following exception:

Unsupported expression for CPS transformation @ line -1, column -1

The Groovy class to which the link is referenced:

final class Bindings implements Serializable{
    @Delegate
    private final Map map

    Bindings(Map map) {
        this.map = map
    }

    @Override
    boolean containsKey(Object key) {
        return true
    }

    @Override
    Object get(Object key) {
        return map.getOrDefault(key, null)
    }
}

You can reproduce an exception in the pipeline or in the library like this:

def map = ["key":"value"]
Bindings bindings = new Bindings (map) 

回答1:


It's a known Jenkins issue: JENKINS-45901

This has been open since August 2017. Looks like it isn't going to be fixed anytime soon:

Not sure if there is any particular place where Groovy language support (or lack thereof) is exhaustively documented, but at any rate I would not expect this issue or anything like it to be addressed. The focus going forward is allowing external process execution, not wasting any more time on the CPS engine except in cases of security vulnerabilities or serious regressions.

I tried the combination of @Delegate with @NonCPS but the latter doesn't seem to have any effect on fields.

You'll have to implement the delegate methods yourself or try another solution.




回答2:


Not sure offhand what the issue here is (other annotations do work). Maybe only applies to source-generating annotations. Anyway for any non-trivial library it will most likely make use of Groovy language features Sorry but exotic features are not generally available in Pipeline script. Lack of support for Groovy class annotations is not explicitly documented (as far as I can see) either in this plugin or in the workflow-cps-plugin https://github.com/jenkinsci/workflow-cps-plugin.



来源:https://stackoverflow.com/questions/50356156/groovy-delegate-class-annotations-are-not-supported-in-the-pipeline-shared-libr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!