I use the Google Reflection Library to request specific resources in the classpath. These resources are located in the same place as 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 run them with Maven (e.g. with maven install ), they do not work properly. After some debugging, apparently the problem is that when executed with Maven, the Reflections library cannot find the URL of the class where the resources are located.
I came to this conclusion by examining how Reflections defines the class URLs that should be checked. As an example, the following method shows how Reflections finds the available class path URLs specified by the class loader (the original Reflections method was slightly simplified):
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 moves through the classloader hierarchy, requesting the URLs of each individual classloader.
When in Eclipse I call the previous method from 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 see (among many other URLs) the URLs of the classes that are configured as part of my project:
file:<MY_PROJECT_PATH>/target/classes/ file:<MY_PROJECT_PATH>/target/test-classes/
and Reflections works like a charm (the resources that need to find reflections are in file:<MY_PROJECT_PATH>/target/classes/ ).
However, when the test was run by Maven, I realized that these URL entries are not in the set returned by the forClassLoader method, and the rest of the Reflections methods do not work properly for this problem.
Itβs βamazingβ that if I write this when unit test runs maven:
ClassLoader myClassClassLoader = <MyClass>.class.getClassLoader(); url = myClassClassLoader.getResource("anExistingResource"); System.out.println("URL: "+url);
I see that the class loader can still resolve the resource I'm trying to find. I am puzzled by the fact that when running with Maven, the forClassLoader method forClassLoader not include the paths to my project in the returned set of URLs, although at the same time it can resolve resources located at such URLs (!).
What is the reason for this behavior? Is there any workaround that I can try to make the Reflections library when called as part of the unit test executed by Maven?