I am trying to dynamically extract configuration from a given WAR file.
My goal is to find all class that implement some interface (Parameter
).
The war file is not on the classpath, so I create a temporary classloader to inspect its classes.
URL webClasses = new URL("jar","","file:" + new File(path).getAbsolutePath() + "!/WEB-INF/classes/");
URLClassLoader cl = URLClassLoader.newInstance(new URL[] { webClasses });
Reflections reflections = new Reflections(ClasspathHelper.forClassLoader(cl), new SubTypesScanner(), cl);
Set<Class<? extends Parameter>> params = reflections.getSubTypesOf(Parameter.class);
This works fine. I find all the implementation of Parameter
in my webapp.
Now, I would like to do the same for each jar present in WEB-INF/lib
.
Unfortunatelly I have not found a way to build the classloader in that case. The classloader seems to ignore jar URLs (jar:<url>!/WEB-INF/lib/{entry}.jar
).
When I run the code below, there is no class found by Reflections:
Set<URL> libs = findLibJars(warUrl));
// The URLs are in the following format : {myWar}.war!/WEB-INF/lib/{myLib}.jar
URLClassLoader cl = URLClassLoader.newInstance(urls.toArray(new URL[urls.size()]));
cl.loadClass("my.company.config.AppParameter");
If possible, I would like to avoid having to extract WEB-INF/lib/*.jar
from the WAR file and analyze them separately.
I am missing something here ? (other way to do it, other way to create the classloader, ... any lead would help). Maybe this can be done without using the Reflections library ?