Groovy: How is the inclusion of @Grab different from the inclusion of classpath?

1. As a rule, how does the inclusion of @ Grape / @ Grab differ from the inclusion of classpath?

2. In particular, what could cause the following difference in behavior?

I have a requirement for xpp3 that 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 extracted by Grape to my Groovy path :

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

... then everything works.

+6
source share
1 answer

Grab uses ivy to retrieve the specified library (plus all its dependencies) from the maven main repository. He then adds these loaded libraries to the path of the bootloader class, which works with the tge current script.

Adding jar to the classpath simply adds the specified jar to the system path.

Since there are no dependencies in this example, this is probably a requirement for the library to be loaded by the system class loader.

To check this, try adding

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

Instead of a single line, take now

+8
source

Source: https://habr.com/ru/post/958168/


All Articles