如何在 Jenkins 管道中为 Groovy 中的关键字 'use' 制定解决方法?

How to make a workaround for the keyword 'use' in Groovy in a Jenkins Pipeline?

我有 groovy 应用程序,我正在使用以下代码进行一些 json 验证工作:

 String method = "{\n" +
                "\"method\":\"filesContentReplacer\"," +
                "\"filesContentReplacer\":{\n" +
                "\"files\":[" +
                "{" +
                "\"path\":\"pom.xml\"," +
                "\"target\":\"1.9.0\"," +
                "\"replacement\":\"STF_SomeNumber\"" +
                "}" +
                "]" +
                "}" +
                "}"
        def json = new JsonSlurper().parseText(method)
        use(JsonSchema){
            assert json instanceof Map
            json.schema = (Map) executeReadJson([text: readFromResources('methods/filesContentReplacer.json')])
            Log.instance.debug( "json.schema:${json.schema}")
            assert json.conformsSchema()
        }

这是纯 groovy 代码,基于此 git 存储库:https://github.com/alexdeleon/groovy-json-schema

问题是当我 运行 jenkins 管道上的代码时,它抛出以下错误:

expected to call org.codehaus.groovy.runtime.GroovyCategorySupport.use but wound up catching org.jenkinsci.plugins.workflow.cps.CpsClosure2.call; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/

经过一番研究,原来制作这个流水线groovy语言的人,没有把他们该有的都做出来,不支持关键字'use'。

现在我需要以某种方式让它工作。你能帮帮我吗?

此外,如果您需要更多代码,我会提供。但我基本上只是将 git 中的 2 类 复制到我的项目中。

感谢您的宝贵时间。

没有要测试的 jenkins,但您可以在这里尝试:

1/

org.codehaus.groovy.runtime.GroovyCategorySupport.use(JsonSchema){...}

2/

@NonCPS
def myConformsSchema(String json, String schema){
  ...here put your code to validate json against schema
}

3/

您可以尝试使用 https://github.com/alexdeleon/groovy-json-schema/blob/master/src/main/groovy/com/lumata/os/groovy/jsonschema/JsonSchema.groovy

中的静态方法

像这样:

def schema = executeReadJson(...)
assert JsonSchema.conformsSchema(json, schema)