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
}
Related
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)
}
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
I am trying to run geb tests through gradle. I've these installed
java version "1.8.0_31"
Groovy Version: 2.4.0
Gradle 2.3
But i am getting this error when running the test.
Exception in thread "main" java.lang.NoClassDefFoundError: java/util/HashMap$Ent
ry
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
Running a simple groovy script with above setup and the below Build.gradle is fine.
Build.gradle looks like this:
buildscript {
repositories {
jcenter()
}
}
apply plugin: 'java'
apply plugin: 'groovy'
repositories {
jcenter()
mavenCentral()
}
dependencies {
def seleniumVersion = "2.45.0"
def phantomJsVersion = '1.1.0'
// selenium drivers
compile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion"
compile "org.seleniumhq.selenium:selenium-support:$seleniumVersion"
compile("com.github.detro.ghostdriver:phantomjsdriver:$phantomJsVersion") {
transitive = false
}
// geb
compile 'org.codehaus.geb:geb-core:0.7.2'
compile 'org.codehaus.geb:geb-spock:0.7.2'
// spock
compile 'org.spockframework:spock-core:0.6-groovy-1.8'
//junit
testCompile group: 'junit', name: 'junit', version: '4.7'
}
task runGebScript (dependsOn: 'classes', type: JavaExec) {
main = 'test'
classpath = sourceSets.main.runtimeClasspath
}
Can someone please help.
According to Gradle dependency report for the build you included, the Groovy version used for testCompile configuration is 1.8.5. Only groovy 2.x is JDK8 compatible. I would do as ataylor suggests and bump Spock version. If you use 1.0-groovy-2.4 then you'll be using Groovy 2.4.1. Also, I would suggest updating version of Geb while you're at it - the latest is 0.10.0. Your dependency versions are way out of date.
Something is trying to use the inner class HashMap.Entry, which no longer exists in Java 8. It's difficult to tell where without a complete stack trace.
However, you're using this version of spock: org.spockframework:spock-core:0.6-groovy-1.8. That version is not compatible with groovy 2.0+. Try updating the dependency to org.spockframework:spock-core:1.0-groovy-2.4.
I wrote some groovy code, compiled using compile localGroovy() and published the jar to artifactory.
Now I wrote a gradle plugin, where I have compile localGroovy() and compile "gav of jar above"
I build my plugin and publish to same artifactory.
Now,
To use the above custom plugin, I am having
buildscript {
dependencies {
classpath localGroovy()
classpath "gav of plugin", transitive: true
}
}
As you see, I'm using same localGroovy() everywhere. My gradle version is also same. Now with above, I get the below error:
Caused by: java.lang.NoClassDefFoundError: org/codehaus/groovy/runtime/typehandling/ShortTypeHandling
at com.oracle.emdi.tools.lrgmanager.LrgManager.<clinit>(LrgManager.groovy:44)
at com.oracle.emdi.gradle.testinfra.tasks.PrintLrg.class$(PrintLrg.groovy)
at com.oracle.emdi.gradle.testinfra.tasks.PrintLrg.$get$$class$com$oracle$emdi$tools$lrgmanager$LrgManager(PrintLrg.groovy)
at com.oracle.emdi.gradle.testinfra.tasks.PrintLrg.print(PrintLrg.groovy:38)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:219)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:212)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:201)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:533)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:516)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
As I read in this page and here the issue is related to groovy / gradle version mismatch. But this is not the case here
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