I have a project with the following structure:
+---main
| \pom.xml
+---module1
| \pom.xml
+---module2
| \pom.xml
+---module3
| \pom.xml
+---module4
| \pom.xml
+---pom.xml
The modules uses ServiceLoader
mechanism to register their services. The main class is in main
module which is also responsible for loading the services in classpath using ServiceLoader
mechanism. The problem is that the main
module is not aware of module1
, module2
, module3
and module4
because they're installed by the user independently. What I want to to is to add all the classes in these modules to classpath when starting the program so that ServiceLoader will be aware of the services in these modules.
Currently, I'm loading these modules at runtime from main
module but it doesn't seem to be good way of solving this problem because the IDE doesn't aware of these modules and it seems that it takes much more time to load these modules at runtime. I have looked at how modular Java projects like Elasticsearch and PrestoDB handle this problem but it seems they both load the installed modules at runtime.
How do you handle this problem when developing modular Java applications? Let's say I have a directory called plugins that has the jar files of the modules the user installed, is there any way to start the application with the classpath that contains all the jars in that directory using a maven plugin? Or do I have to load these jars at runtime?