Maven – fixing classpath issues for tests

We had problems with tests not finding files that we usually package outside the jar. One of such files was log4j.xml and other files were configuration files for spring framework. We need to pass full path to the log4j file, so that we don’t have to rely on class path priorities to look it up. Given that log4j.xml is such a widely used file, it is probable that one of the dependent jar would have a copy in the archive – so we can pick up the wrong file by mistake. On the other hand, we want to have our spring framework configuration files in the class path, but outside the jar. This way we can modify the configuration on the fly if we need to. So to setup integration test case, we needed to get the full path to the log4j.xml file and adding a directory to the class path for test only.
Here is how to do that:
public static String getFullPath(Class c, String name){
     ClassLoader cl = c.getClassLoader();
     URL url = cl.getResource(name);
     return url != null ? url.getPath() : null;
and for adding class path:

Leave a Reply

You must be logged in to post a comment.