Jenkins XmlParser报告未找到根节点属性的此类字段 [英] Jenkins XmlParser reports No such field found for attribute for root node
问题描述
我有一个项目,该项目具有以下XML文件(Cordova项目的config.xml)...
I have a project that has the following XML file (config.xml for a Cordova project) ...
<?xml version='1.0' encoding='utf-8'?>
<widget android-versionCode="16" id="com.mycomp.myapp" ios-CFBundleVersion="15" version="1.3.0.b4" windows-packageVersion="1.2.6.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>My App</name>
<description>My app description</description>
<author>mycom.com.au</author>
....
我要做的就是读取根元素(widget
)的version
属性值(为我提供字符串1.3.0.b4
).遵循此处的示例使用.@
获取属性.
All I want to do is read the value of the version
attribute (to give me the string 1.3.0.b4
) of the root element (widget
). Following the example here where it says to use the .@
to get an attribute.
我的Jenkins文件脚本中包含以下内容...
I have the following in my Jenkins file script...
script {
def xml = readFile "${env.WORKSPACE}/config.xml"
def rootNode = new XmlParser().parseText(xml)
def version = rootNode.@version
echo 'version is...'
echo version
但是当我运行它时,出现以下错误.
But when I run it, I get the following error..
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node version
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetAttribute(SandboxInterceptor.java:436)
at org.kohsuke.groovy.sandbox.impl.Checker$8.call(Checker.java:370)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetAttribute(Checker.java:375)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getAttribute(SandboxInvoker.java:37)
我已经尝试过rootNode.@version
(如上所述)rootNode[0].@version
和rootNode[3].@version
,但是没有任何效果.
I have tried rootNode.@version
(as above) rootNode[0].@version
and rootNode[3].@version
but nothing works.
有人对以上问题有任何想法吗?
Does anyone have any idea of whats wrong with the above?
预先感谢
如果我使用以下内容...
If I use the following...
def xml = readFile "${env.WORKSPACE}/config.xml"
def rootNode = new XmlParser().parseText(xml)
def version = rootNode.text()
echo 'version is...'
echo version
它打印出My app description
有点奇怪(它跳到了描述节点)
it prints out My app description
which is a bit weird (it jumps down to the description node)
我尝试使用以下内容.
def rootNode = new XmlSlurper().parse("${env.WORKSPACE}/config.xml")
def version = rootNode.@'version'
但是我仍然遇到类似的错误...
but I still get a similar error...
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.slurpersupport.NodeChild version
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:425)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetAttribute(SandboxInterceptor.java:436)
at org.kohsuke.groovy.sandbox.impl.Checker$8.call(Checker.java:370)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetAttribute(Checker.java:375)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getAttribute(SandboxInvoker.java:37)
at com.cloudbees.groovy.cps.impl.AttributeAccessBlock.rawGet(AttributeAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:15)
at ___cps.transform___(Native Method)
如果我呼叫echo rootNode.text()
,似乎再次只是打印出,widget
主标签内的前3个标签的内容,即My AppMy app descriptionmycom.com.au
.
If I call echo rootNode.text()
, once again it seems to just print out the contents of the first 3 tags inside the main ,widget
tag, i.e. My AppMy app descriptionmycom.com.au
.
推荐答案
在能够修改属性的上下文中,我进行了更多测试,并发现使用[]
访问时,属性的@
选择器实际上是有效的.看来这会导致您使用詹金斯(getAt
和putAt
)认可的不同方法.
I tested some more in context of being able to also modify the attribute, and found out when using the []
access, the @
selector for attributes actually works. It seems this leads to use different methods under the hood which you can approve in jenkins (getAt
and putAt
) .
我们可以简单地使用
def rootNode = new XmlParser().parseText(xml)
println rootNode['@version']
原始答案:
Original Answer:
There seems some bug regarding the direct access to the attributes with the @
selector on the groovy.util.Node object with the script sandbox.
一种解决方法是使用.attributes()
方法获取完整的Map属性,并通过如下所示的键访问值:
A workaround is to use the .attributes()
method to get the full Map of attributes, and access the value via the key like the following:
def rootNode = new XmlParser().parseText(xml)
println rootNode.attributes()['version']
这将使首次运行失败,并提示您批准使用method groovy.util.Node attributes
,但是一旦获得批准,该功能就会生效.
This will fail the first run and promt you to approve the use of method groovy.util.Node attributes
, but once approved will work.
这篇关于Jenkins XmlParser报告未找到根节点属性的此类字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!