Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy: How does @Grab inclusion differ from classpath inclusion?

1. Generally, how is @Grape/@Grab inclusion different than classpath inclusion?

2. Specifically, what might cause the following behavior difference?

I've got a requirement on xpp3 which I express as:

// TestScript.groovy
@Grab(group='xpp3', module='xpp3', version='1.1.3.4.O')
import org.xmlpull.v1.XmlPullParserFactory;
println "Created: " + XmlPullParserFactory.newInstance()

Running $ groovy TestScript.groovy fails with

Caught: org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException:

If, however, I manually add the .jar fetched by Grape to my Groovy classpath:

$ groovy -cp ~/.groovy/grapes/xpp3/xpp3/jars/xpp3-1.1.3.4.O.jar \
         TestScript.groovy 

... then everything works.

like image 710
Bosh Avatar asked Nov 15 '13 20:11

Bosh


People also ask

What is @grab in Groovy?

Standard Groovy codebase contains a @Grab annotation so that dependencies on third-party libraries can be declared. Using @Grab annotation, Grape Dependency Manager downloads jar in similar fashion as that of Maven/Gradle without any build tool. Spring Boot attempts to deduce the required libraries based on code.

What is the role of grape dependency in Groovy?

Grape will, at runtime, download as needed and link the named libraries and all dependencies forming a transitive closure when the script is run from existing repositories such as Maven Central. Grape follows the Ivy conventions for module version identification, with naming change.


1 Answers

Grab uses ivy to fetch the specified library (plus all of its dependencies) from the maven core repository. It then adds these downloaded libraries to the classpath of the loader that's running the current script.

Adding the jar to the classpath just adds the specified jar to the system classpath.

As there are no dependencies in this example, it's probably a requirement that the library needs to be loaded by the system classloader.

To check this, try adding

@GrabConfig(systemClassLoader= true)
@Grab(group='xpp3', module='xpp3', version='1.1.3.4.O')

Instead of the one line Grab you currently have

like image 150
tim_yates Avatar answered Sep 28 '22 20:09

tim_yates