简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM