我想在Jenkins Pipeline中解析XML文件。但是,出现此错误:
无法解析类XmlParser.parseText
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 252: unable to resolve class XmlParser.parseText
@ line 252, column 47.
def test_report_xml = new XmlPar
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:321)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
代码
import groovy.util.XmlParser
pipeline {
stages {
stage ("Parse XML file") {
steps {
def test_report = readFile "report.xml"
def test_report_xml = new XmlParser.parseText(test_report)
println test_report_xml.testsuites.testsuite.each{
println it.@name
}
}
}
}
答案 0 :(得分:1)
我修改了您的代码以使其正常运行(我在控制台脚本上进行了尝试):
import groovy.util.XmlParser
def test_report = readFile "report.xml"
def xml = new XmlParser()
def test_report_xml = xml.parseText(test_report)
唯一的区别是我在使用对象之前创建了对象