What is the best way to load classes to minimize class loader issues?

(andy.jewell) #1

HI. . . I am having trouble (likely making trouble for myself) with loading classes. In my buildSrc I have custom libraries that need to load to support the build classes and custom tasks. In my main build script I need to load some of the same classes. Additionally, I have custom ant tasks that I need to load into the ant classloader that also used in both of the other places.

I was rolling along “ok” for a while, though I seemed to have a ton of duplication. Recently, I made a change and the whole thing is tumbling down with frankenstein errors like:

> loader constraint violation: when resolving method "org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$
Lgroovy/lang/MetaMethod;[Ljava/lang/Class;)V" the class loader (instance of org/gradle/internal/classloader/Mu
tableURLClassLoader) of the current class, org/codehaus/groovy/runtime/callsite/PojoMetaMethodSite, and the cl
ass loader (instance of java/net/URLClassLoader) for resolved class, org/codehaus/groovy/runtime/callsite/Pojo
MetaMethodSite$PojoCachedMethodSiteNoUnwrap, have different Class objects for the type org/codehaus/groovy/run
time/callsite/CallSite used in the signature

So I thought I’d tear it all apart and do it right but can’t find any documentation about doing it right. Can someone please advise on what the best approach is to loading classes (maybe with a hierarchy of classloaders)? Or perhaps refer me to a reference I missed.