Groovy Geb - groovy-xml module conflick - groovy

I am trying to run a simple Groovy script that utilizes Geb to fetch a title. However, I receive an error:
Module [groovy-xml is loaded in version 4.0.2 and you are trying to load version 3.0.8
I user Groovy version 4.02. GROOVY_HOME is set; I installed Groovy via sdkman.
$ groovy -version
Groovy Version: 4.0.2 JVM: 17.0.3 Vendor: Amazon.com Inc. OS: Linux
$ java -version
openjdk version "17.0.3" 2022-04-19 LTS
// #Grab(group='org.gebish', module='geb-core', version='5.1')
#Grapes([
#Grab("org.gebish:geb-core:5.1"),
#Grab("org.seleniumhq.selenium:selenium-firefox-driver:3.141.59"),
#Grab("org.seleniumhq.selenium:selenium-support:3.141.59")
])
import geb.Browser
Browser.drive {
go "http://example.com"
println title
}
How to fix this?

Related

Could not find or load main class org.codehaus.groovy.tools.GroovyStarter

I have this error coming from groovy -v.
Error: Could not find or load main class org.codehaus.groovy.tools.GroovyStarter
Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.tools.GroovyStarter
java version = openjdk version "17.0.6" 2023-01-17 LTS
got that from Microsoft https://learn.microsoft.com/en-us/java/openjdk/install
Groovy version 4.0.8
got groovy from jfrog.io
apache-groovy-binary-4.0.8.zip 22-01-23 01:00:24
Have set Java_home and Groovy_home. Both are appended to the path.
C:\Users<Myusername>\dev\languages\apache-groovy-sdk-4.0.8\groovy-4.0.8\bin
groovy was unzipped into this folder.
I was trying to start the use of groovy by checking its version number.

In Groovy, How do I fix this error: groovy.util.slurpersupport.GPathResult

Given the following code snippet:
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org'); // <<< THROWS EXCEPTION
println http
How can I fix the following error?
Caught: java.lang.NoClassDefFoundError: groovy/util/slurpersupport/GPathResult
java.lang.NoClassDefFoundError: groovy/util/slurpersupport/GPathResult
at java.desktop/com.sun.beans.introspect.MethodInfo.get(MethodInfo.java:70)
at java.desktop/com.sun.beans.introspect.ClassInfo.getMethods(ClassInfo.java:80)
at groovyx.net.http.ParserRegistry.<init>(ParserRegistry.java:87)
at groovyx.net.http.HTTPBuilder.<init>(HTTPBuilder.java:194)
at HttpBuilder.run(HttpBuilder.groovy:4)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Caused by: java.lang.ClassNotFoundException: groovy.util.slurpersupport.GPathResult
... 8 more
Versions of Groovy/Java/Gradle/Maven I'm using
I'm using Groovy 4.0 with JDK 17.0.2 as shown next:
groovy --version
Groovy Version: 4.0.0 JVM: 17.0.2 Vendor: Oracle Corporation OS: Windows 10
java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
mvn --version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: D:\p\apache-maven-3.8.4
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
gradle --version
------------------------------------------------------------
Gradle 7.4
------------------------------------------------------------
Build time: 2022-02-08 09:58:38 UTC
Revision: f0d9291c04b90b59445041eaa75b2ee744162586
Kotlin: 1.5.31
Groovy: 3.0.9
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 17.0.2 (Oracle Corporation 17.0.2+8-LTS-86)
OS: Windows 10 10.0 amd64
Searching for solutions
I found this question: Groovy built-in REST/HTTP client? gave me a work around (not using http-builder).
Background on question
The UDEMY Groovy course https://www.udemy.com/course/apache-groovy/ provides some sample code during one of the lectures on a REST-based client in Groovy that uses the http-builder library.
The groovy and java version being used at the time of the course where
Java Version: 1.8.0_60
Groovy Version: 2.4.5
Gradle: 2.7
Maven: 3.3.3
Spring Boot: 3.0.M5
Spring Tool Suite (STS): 3.7.1.RELEASE-e4.5.1
On Windows 7 and Mac OS ?
i'm sure you have new version of groovy (for example 4.0.1) and http-builder library you are using is quite old.
starting from groovy 3.0 GPathResult class moved to another package: groovy.xml.slurpersupport.GPathResult
but according to error HTTPBuilder is looking for old package groovy.util.slurpersupport.GPathResult
option 1:
You have to downgrade groovy version
option 2: hacking
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
import groovyx.net.http.*
//define empty class with old name to prevent failure
this.getClass().getClassLoader().getParent().parseClass '''
package groovy.util.slurpersupport
class GPathResult{}
'''
def http = new HTTPBuilder('http://httpbin.org')
//redefine xml parser to use xml slurper from a new package
//you don't need this if you are not going to work with xml
http.parser['application/xml']={HttpResponseDecorator r->
return new groovy.xml.XmlSlurper().parse(r.entity.content)
}
http.get(path:'/xml',query:[a:123]){resp,body->
println "status: ${resp.statusLine}"
println groovy.xml.XmlUtil.serialize(body)
}

Groovy #Grab NoClassDefFoundError: org/apache/ivy/plugins/resolver/DependencyResolver

Using #grab for the first time (new to groovy)
I understand it is meant to work "out the box."
However, when I add this to my class:
#Grab(group='commons-lang', module='commons-lang', version='2.4')
I get the following compilation error:
Caused by: java.lang.NoClassDefFoundError: org/apache/ivy/plugins/resolver/DependencyResolver
Groovy version is Groovy Version: 3.0.4 JVM: 11.0.1 Vendor: Oracle Corporation OS: Mac OS X
you have missing dependency or library ivy-2.4.0.jar
this library is a part of groovy-all artifact. check groovy-all.pom to see all groovy dependencies/features
so, you could setup dependency to groovy-all artifact in your project
or to a separate ivy-2.4.0.jar artifact if you don't want to include all groovy features into your project
Try adding this to gradle.build
configurations {
ivy
}
dependencies {
ivy "org.apache.ivy:ivy:2.4.0"
...
}
tasks.withType(GroovyCompile) {
groovyClasspath += configurations.ivy
}

Gradle Groovy compilation cannot find javafx classes in tests

I have the following Groovy test (not the real one, mind you) in a Gradle project (under src/test/groovy):
import javafx.scene.paint.Color
import org.junit.Test
class MyTest {
#Test
void test1() {
assert Color.AQUAMARINE != Color.BLUE
}
}
The build file is as simple as it gets and declares that I use Java 8 (so JavaFX is in the classpath, always):
apply plugin: 'groovy'
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
...
It also has a test dependency on Groovy:
testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.4'
Everything works fine, except when I refer to some JavaFX class in the Groovy test.
It fails with this error:
MyTest.groovy: 3: unable to resolve class javafx.scene.paint.Color
# line 3, column 1.
import javafx.scene.paint.Color
^
The Java code compiles fine and I can run my JavaFX application. Only the Groovy tests seem to not be able to see JavaFX.
I've already tried changing the compileJava block to compileGroovy, compileTestGroovy, also together with the compileJava block, so that the Groovy compiler sees JavaFX 8, but nothing helped.
How can I fix this?
Gradle version:
------------------------------------------------------------
Gradle 2.2
------------------------------------------------------------
Build time: 2014-11-10 13:31:44 UTC
Build number: none
Revision: aab8521f1fd9a3484cac18123a72bcfdeb7006ec
Groovy: 2.3.6
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_60 (Oracle Corporation 25.60-b23)
OS: Linux 3.16.0-38-generic amd64
Changed the Gradle version to 2.7 (latest as of today) and it all works.
If you have the same problem, just use Gradle's wrapper and set the Gradle version to the latest version (they move fast, so bugs will exist, but also get fixed quickly).
gradle wrapper --gradle-version 2.7

Checking Groovy version Gradle is using

I am running gradle and have previously been running groovy 1.76. I have now updated to groovy on my local machine (groovy_home points to groovy 2.1.2 etc).
$ groovy -version
Groovy Version: 2.1.2 JVM: 1.7.0_17 Vendor: Oracle Corporation OS: Linux
However, when I am running gradle commands (gradle test, classes, etc) I believe it is not building against groovy 2.1.2 but is actually still building against 1.76. (The reason I believe this, is that when I execute the classes I keep getting this error Upgrading Groovy 1.7 - 2.1 Incompatability, which is related to changes made post 1.76)
Is there a way to confirm which version of groovy my gradle install is building against?
Also, can anyone confirm where I should be configuring the groovy version for gradle?
While trying to check the groovy version during gradle runtime, I found you can also print the Groovy version:
task version {
doLast {
println "Gradle version: " + project.getGradle().getGradleVersion()
println "Groovy version: " + GroovySystem.getVersion()
}
}
As examples:
$ ~/usr/gradle-1.8/bin/gradle -q version
Gradle version: 1.8
Groovy version: 1.8.6
$ ~/usr/gradle-2.1/bin/gradle -q version
Gradle version: 2.1
Groovy version: 2.3.6
Note.- GroovySystem.getVersion() is available since Groovy 1.6.9
Which Groovy library you are building against (and which Groovy compiler you are using) is determined by which Groovy library resides on the compile (or, in earlier Gradle versions, groovy) configuration. Typically a Groovy dependency is configured explicitly, but it may also be pulled in by transitive dependency management. (In case of a version conflict, the higher version wins by default. Which Groovy version(s) you have installed on your machine is irrelevant.) gradle dependencyInsight --configuration compile --dependency groovy should provide the answer.
Here is how a Groovy dependency is typically configured:
apply plugin: "groovy"
repositories {
mavenCentral() // or some other repository containing a Groovy library
}
dependencies {
// in Gradle 1.4 or earlier, replace 'compile' with 'groovy'
compile "org.codehaus.groovy:groovy-all:2.1.2"
}
in windows you can check it using: gradlew --v
------------------------------------------------------------
Gradle 6.2
------------------------------------------------------------
Build time: 2020-02-17 08:32:01 UTC
Revision: 61d3320259a1a0d31519bf208eb13741679a742f
Kotlin: 1.3.61
Groovy: 2.5.8
Ant: Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM: 15.0.2 (Oracle Corporation 15.0.2+7-27)
on linux the command will be : gradle --v
I'm using osx
gradle --v
------------------------------------------------------------
Gradle 7.5.1
------------------------------------------------------------
Build time: 2022-08-05 21:17:56 UTC
Revision: d1daa0cbf1a0103000b71484e1dbfe096e095918
Kotlin: 1.6.21
Groovy: 3.0.10
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 17.0.4.1 (Homebrew 17.0.4.1+1)
OS: Mac OS X 12.4 x86_64

Resources