java.io.NotSerializableException:groovy.util.slurpersupport.Attributes

时间:2018-03-13 04:44:39

标签: jenkins jenkins-pipeline

我正在尝试运行此管道脚本

#!groovy

@NonCPS
def findCommand(filePath) {
    def file = new File(filePath)
    def text = file.getText()
    def components = new XmlSlurper().parseText( text )

    def map = new HashMap<>()

    components.component.each { def component->
        map << ["${component.@application}" : [:]]
        def componentMap = map.get("${component.@application}")
        component.environments.environment.each { def environment->
            componentMap << ["${environment.@name}" : [:]]
            def actionMap = componentMap.get("${environment.@name}")
            environment.actions.action.each { def action->
                actionMap << ["${action.@toDo}" : action]
            }
        }
    }

    components = null
    text = null
    file = null

    return map
}

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building..'
                echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"
                echo "Parameters: ${params.environment} ${params.actions} ${params.applications}"

                script{
                    try {
                         map = findCommand("XXX.xml")
                    } catch (Exception e) {
                        echo "Catching the NonCPS Exception";
                    }
                    echo "${map}"
                }

                build job: "sample echo", parameters:   [ 
                                                            [$class: 'StringParameterValue', name: 'application', value: "${params.application}"], 
                                                            [$class: 'StringParameterValue', name: 'environment', value: "${params.environment}"]
                                                        ]
            }
         }
     }
     post {
         failure {
             echo "Build failed..."
         }
     }

}

我知道我正在使用XmlSlurper,但我还指定了代码块为@NonCPS。但是,这仍然是抛出java.IO.NonSerializableException。

我也尝试使用try-catch块来捕获异常,但这似乎也没有用。 我很乐意在如何使构建完全干净方面获得一些帮助。谢谢:(

1 个答案:

答案 0 :(得分:1)

答案是例外。这很奇怪,但正在改变

components.component.each { def component->
        map << ["${component.@application}" : [:]]
        def componentMap = map.get("${component.@application}")
        component.environments.environment.each { def environment->
            componentMap << ["${environment.@name}" : [:]]
            def actionMap = componentMap.get("${environment.@name}")
            environment.actions.action.each { def action->
                actionMap << ["${action.@toDo}" : action]
            }
        }
    }

components.component.each { def component->
        def application = "${component.@application}".toString()
        map << [ (application) : [:]]
        def componentMap = map.get(application)
        component.environments.environment.each { def environment->
            def name = "${environment.@name}".toString()
            componentMap << [ (name) : [:]]
            def actionMap = componentMap.get(name)
            environment.actions.action.each { def action->
                def toDo = "${action.@toDo}".toString()
                def actionString = action.toString()
                actionMap << [ (toDo) : (actionString)]
            }
        }
    }

解决了这个问题。它与仍然被携带到我的HashMap中的(不可序列化的)XMLSlurper数据结构类型有关。