Search code examples
javaclassloadermaven-pluginmulti-module

classLoader can't load classes implementing a class out of this module


I'm trying to load specific classes in my maven plugin using below class loader:

 public ClassLoader getClassLoader(MavenProject project) {
    try
    {
        List classpathElements = project.getCompileClasspathElements();
        URL urls[] = new URL[classpathElements.size()];
        for ( int i = 0; i < classpathElements.size(); ++i ) {
            urls[i] = new File( (String) classpathElements.get( i ) ).toURL();
        }
        return new URLClassLoader( urls, this.getClass().getClassLoader() );
    } catch ( Exception e ) {
        System.out.println( "Couldn't get the classloader." );
        return this.getClass().getClassLoader();
    }
}

this loader works completely fine in a test simple project. but when I use it in a multi-module project it doesn't load specific classes. the classes that implement a class in another module(for example CardlessFacadeBean implements CardlessFacade, CardlessFacadeBean class is in this module and CardlessFacade class is in another module). but other classes that don't have this condition loads fine. is there any way to solve this issue in a simple way? Thanks so much


Solution

  • I simply added another module classpath to classpath Elements list as below and it recognized mentioned classes.

    classpathElements.add(moduleDirectory);
    

    any other solution would be appreciated.