I have a code generator that uses URLClassLoader to load classes on a specified path, scan them for annotations, and then using Reflection on the fields/methods, generate DTOs.
It works great, in the test app.
When I put it into the Maven MOJO, I suddenly lose the ability to see the javax.persistence.Entity annotations on the classes. It loads them, it can see all the fields, but the Entity annotation is no longer visible.
I am assuming this is something to do with Classpath issues – is it? Neither the test app (a main() function in the plugin itself) or the MOJO are part of the project that the scanned classes are from. But one works and the other doesn’t.
I have a little bit of debug code that prints out all of the annotations on the class when it examines them, and in the non-running version it finds literally none.
Any ideas how I debug the problem/solve it?
The problem turned out to be pretty simple, although I’m not sure why it worked fine in one case and not in another.
My URLClassLoader creation didn’t specify a parent classloader. So, I assume it couldn’t find anything. As soon as I used
for the classloader, it all started working just fine. I’m pretty ignorant when it comes to the ins and outs of classloaders, so this wasn’t obvious. Especially since the example I was following didn’t specify a parent either.