Groovy version for XmlParser


(Michael Brand) #1

I am having trouble working with an XML file in my Gradle script. The XML functionality works fine outside of my multi-project workspace, but within the workspace I get an error that looks like it’s caused by using an old version of Groovy. Here is my test code:

import groovy.xml.*
  def xml = """
<a>
 <b c="d" e="f"/>
 <b c="g" e="h"/>
</a>
"""
  def a = new XmlParser().parseText(xml)
println a.b.find{it.@e == "f"}.@c
  a.b[0].@c = "z"
println XmlUtil.serialize(a)
  a.b.find{it.@c == "g"}.@e = "y"
println XmlUtil.serialize(a)

To test, I’ve copied my gradlew and the “gradle” folder from my workspace to a temporary location. I’ve then copied this script, so the listing is:

mbrand@mac:~/tmp/gradleTest$ ls
quickTest.gradle gradle
gradlew

Running “./gradlew -b quickTest.gradle” runs successfully.

If I copy this to my multi-project workspace and run the same command, the build fails with:

FAILURE: Build failed with an exception.
  * Where:
Build file '/Users/mbrand/ws/serverConfig/quickTest.gradle' line: 11
  * What went wrong:
A problem occurred evaluating root project 'serverConfig'.
> No such field: e for class: groovy.util.Node

This looks like it’s accessing a very old version of Groovy. The question is, where is getting this version setting from? I can’t find anywhere in my code that I define the Groovy version. I’ve used the same Gradle wrapper in both locations.

Any idea what’s going on here or how I can resolve it?


(Peter Niederwieser) #2

Maybe the build declares a plugin or library using ‘buildscript { dependencies { classpath … } }’ that transitively pulls in an old version of Groovy?


(Michael Brand) #3

Is there anyway to find it? I’ve searched my buildSrc plugins and didn’t find anything there.

Alternatively, can I explicitly override the transitive version? I’ve tried defining dependencies on Groovy at various locations (subproject, buildscript{}, main build.gradle, etc.), but haven’t managed to get it right yet.


(Peter Niederwieser) #4

Something like this should work:

allprojects {
    task findGroovy << {
        buildscript.configurations.classpath.findAll { it.name.contains "groovy" }.each { println it }
    }
}

Excluding transitive dependencies should also work, either from a parent script or from within the ‘buildscript {}’ block.


(Michael Brand) #5

I’ll give this a try when I can. I resolved the issue by using XmlSlurper instead of XmlParser. I’m not sure why I chose the parser to begin with. I usually use the slurper and its format is more appropriate anyway.

Thanks again for you help, Peter.