我在Jenkinsfile函数中使用@NonCPS,因为我需要使用XmlSlurper获取XML的属性,即使使用@NonCPS注释,我仍然会收到java.io.NotSerializableException错误。
下面是我的代码
@Field prBranchCoverage
@NonCPS
def xmlCovergeParse(CoverageXml) {
println "start coverage"
def prParser = new XmlSlurper()
prParser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
prParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
def prCovergeDoc = prParser.parseText(CoverageXml)
def branchCoverage = prCovergeDoc['@line-rate']
return branchCoverages
}
stage('Pr coverage ') {
sh "mvn -V -B -U cobertura:cobertura -PcodehausCoverage -Dmaven.test.failure.ignore=true -DskipWeaving=true test"
publishTestResults cobertura: [archive: true,pattern: "**/target/site/cobertura/coverage.xml"]
def CoverageXml = readFile file: './target/site/cobertura/coverage.xml'
def prBranchCoverage = xmlCovergeParse(CoverageXml)
echo "start coverage"
println prBranchCoverage
}
我得到的错误是:
Error: java.io.NotSerializableException: groovy.util.slurpersupport.Attributes
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@72e69aed
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@1a22d024
in field com.cloudbees.groovy.cps.Continuable.e
有人可以帮忙吗?
答案 0 :(得分:1)
您的方法xmlCovergeParse(CoverageXml)
返回不可序列化的对象
jenkins要求管道中的所有变量均为Serializable,因为下一步可以在其他节点上执行,而不是在前一个节点上执行。因此要在节点之间传输变量,它们将被序列化。
只需在您的方法return branchCoverages.toString()
中添加xmlCovergeParse