I am using the Google Reflections library for querying certain resources in the classpath. Those resources are located in the same location than the classes in my project.
I wrote some unit tests that succeed when executed as a unit test in Eclipse, but when I try to execute them with Maven (with a maven install
for example), they are not working as expected.
After some debugging, apparently the problem is that when executed with Maven, the Reflections library cannot find the classpath url where the resources are located.
I arrived to that conclusion researching how Reflections determines the classpath URLs that should be inspected. As an example, the following method shows how Reflections finds the available classpath URLs given a class loader (the original Reflections method has been simplified a bit):
public static Set<URL> forClassLoader(ClassLoader... classLoaders) {
final Set<URL> result = Sets.newHashSet();
for (ClassLoader classLoader : classLoaders) {
while (classLoader != null) {
if (classLoader instanceof URLClassLoader) {
URL[] urls = ((URLClassLoader) classLoader).getURLs();
if (urls != null) {
result.addAll(Sets.<URL>newHashSet(urls));
}
}
classLoader = classLoader.getParent();
}
}
return result;
}
In short, it is traversing the class loader hierarchy asking for the URLs of each individual classloader.
When in Eclipse I invoke the previous method from a unit test with something like this:
ClassLoader myClassClassLoader = <MyClass>.class.getClassLoader(); //<MyClass> is in the same classpath url than the resources I need to find
Set<URL> urls = forClassLoader(myClassClassLoader);
for(URL url : urls) {
System.out.println("a url: " + url);
as expected, I can see (among many other URLs) the classpath URLs that are configured as part of my project:
file:<MY_PROJECT_PATH>/target/classes/
file:<MY_PROJECT_PATH>/target/test-classes/
and Reflections works as a charm (the resources Reflections should find are located in file:<MY_PROJECT_PATH>/target/classes/
).
However, when the test is executed by Maven, I realized that these URL entries are missing from the set returned by the forClassLoader
method, and the rest of the Reflections methods are not working as expected for this problem.
The "surprising" thing is that if I write this when the unit test is executed by maven:
ClassLoader myClassClassLoader = <MyClass>.class.getClassLoader();
url = myClassClassLoader.getResource("anExistingResource");
System.out.println("URL: "+url); //a valid URL
I can see that the class loader still can resolve the resource I am trying to find.
I am puzzled about why when executed with Maven the forClassLoader
method does not include in the returned set the classpath URLs of my project, although at the same time it is able to resolve resources that are located in such urls(!).
What is the reason of this behavior? Is there any workaround I can try to make the Reflections library work when invoked as part of a unit test run by Maven ?
See Question&Answers more detail:
os