18

I have a groovy script like so:

@Grab('com.univocity:univocity-parsers:2.0.0')
import com.univocity.parsers.csv.*;

class MyCsvParser {

}

And I want to load this class in my java application via GroovyClassLoader. But the @Grab somehow yields in an ivy exception:

SomeJavaClass {
    void someMethod() {
         String script = FileUtils.readFileToString("the groovy File");
         Class c = new GroovyClassLoader(this.getClass().getClassLoader())).parse(script);
    }
}

Stack:

Caused by: java.lang.ClassNotFoundException: org.apache.ivy.core.report.ResolveReport
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 41 more

When I comment out the @Grab everything works just fine. How can I enable the Grapes in GroovyClassLoader?

0

2 Answers 2

26

You should add the ivy dependency. It's not added by default because it's declared as non-transitive. Ivy is the library which manages the dependencies loaded by @Grab:

<dependency>
  <groupId>org.apache.ivy</groupId>
  <artifactId>ivy</artifactId>
  <version>2.4.0</version>
</dependency>
3
  • 11
    Add this dependency to what?
    – Michael
    Jan 22, 2020 at 15:35
  • No seriously, what is this xml file and where is it located?
    – cowlinator
    Mar 1 at 1:54
  • In your pom.xml if you are using maven. Mar 2 at 9:03
0

Add this to your gradle.build

configurations {
  ivy
}

dependencies {
  ivy "org.apache.ivy:ivy:2.4.0"
 ...
}

tasks.withType(GroovyCompile) {
  groovyClasspath += configurations.ivy
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.