简体   繁体   English

Jenkins管道替代XmlSlurper

[英]Jenkins Pipeline alternative for XmlSlurper

I need to create a step in my pipeline to get a Xml file and change specific elements on it (based on variables and other build outputs) before going to the next step. 在进入下一步之前,我需要在我的管道中创建一个步骤以获取Xml文件并更改其上的特定元素(基于变量和其他构建输出)。

Although I could easily create an C# or Java program for doing it, I decided to give it a try to add a new step in my pipeline and deal with the Xml directly. 虽然我可以轻松地创建一个C#或Java程序,但我决定尝试在我的管道中添加一个新步骤并直接处理Xml。

The problem I am facing is that the XmlSlurper is not white-listed yet, according to these posts: 根据这些帖子,我面临的问题是XmlSlurper尚未列入白名单:

  • https://issues.jenkins-ci.org/browse/JENKINS-33024 https://issues.jenkins-ci.org/browse/JENKINS-33024

    XmlSlurper is too problematic and will not be supported. XmlSlurper太有问题,不受支持。 It is sensitive to thread context class loader issues, which can cause mysterious errors from apparently unrelated plugins. 它对线程上下文类加载器问题很敏感,这可能会导致显然不相关的插件出现神秘错误。 Certain methods are also inappropriate to whitelist. 某些方法也不适合白名单。

Currently I have to deal with some files, and it is ok. 目前我必须处理一些文件,这没关系。 The Jenkins Pipeline Utility Steps provide already some functions to read and deal with JSON already. Jenkins管道实用程序步骤已经提供了一些函数来读取和处理JSON。 But there is no alternative for XmlSlurper or anything related to that. 但是没有替代XmlSlurper或与之相关的任何东西。

Unfortunately I am dealing with some tools that I can't change to JSON, and since XML is still largely used, does someone know any alternative to XmlSlurper ? 不幸的是,我正在处理一些我无法更改为JSON的工具,并且由于XML仍然被大量使用, 有人知道XmlSlurper任何替代方案吗?

I would rather not to try do deal with those XML manually, like dealing with regex stuff. 我宁愿不尝试手动处理这些XML,比如处理正则表达式的东西。

Convert the XML to json in a shared library in groovy. 在groovy中将XML转换为共享库中的json。 I have a class that resides in my shared library in src/org/config called GroovyUtils. 我有一个类位于src / org / config中的共享库中,名为GroovyUtils。

package org.config

import groovy.text.SimpleTemplateEngine
import java.nio.charset.StandardCharsets
import groovy.json.JsonSlurper
import org.json.JSONObject
import org.json.XML
import java.util.ArrayList
import hudson.model.*;

public class GroovyUtils implements Serializable {
    def engine = new SimpleTemplateEngine()
    def config
    def steps

    def GroovyUtils(config, steps) {
        this.config = config
        this.steps = steps
    }

    def log(msg){
        steps.println(msg)
    }

    def getFinalAttr(thingToSearchFor){
        def fileContents = new File("YourXMLFile.xml").text;
        def json = xmlToJson(fileContents);
        def jsonObj = new JsonSlurper().parseText(json)
        def foo = jsonObj["someAttr"].someArr["anotherArr"]
        def bar = foo.find { it.name == thingToSearchFor }
        return bar.finalAttr
    }

    def xmlToJson(input) {
        JSONObject xmlJSONObj = XML.toJSONObject(input)
        return xmlJSONObj.toString(2)
    }
}

The key method above is getFinalAttr(thingToSearchFor). 上面的关键方法是getFinalAttr(thingToSearchFor)。

The assignments to foo and bar local variables will depend on the structure of your jsonObj but shows you that it can be traversed to get at what you want. foo和bar局部变量的赋值将取决于jsonObj的结构,但会向您显示可以遍历它以获得您想要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM