Writing Modular Java Applications

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

      

Modules use a mechanism ServiceLoader

to register their services. The main class is in the module main

, which is also responsible for loading services in the pathpath using the mechanism ServiceLoader

. The problem is that the module main

is not aware of module1

, module2

, module3

and module4

, because they are set by the user. I want to add all the classes in these modules to the classpath when the program starts up so that the ServiceLoader knows about the services in these modules.

I am currently loading these modules at runtime from a module main

, but it doesn't seem to be a very good way to solve this problem because the IDE is unaware of these modules and it seems like it takes a lot longer to load these modules at runtime ... I've looked at how modular Java projects like Elasticsearch and PrestoDB deal with this problem, but it looks like they both load installed modules at runtime.

How do you deal with this problem when developing modular Java applications? Let's say I have a directory called plugins that has jar files from modules installed by the user, is there a way to run the application using the classpath that contains all the jars in that directory using the maven plugin? Or do I need to load these jars at runtime?

+3


source to share


1 answer


Usually the user launches the application from a script (or in windows with some custom java exe launcher). In your script, you can simply add the plugin folder to your classpath in the same way:



java -cp plugin/* -jar app.jar

      

0


source







All Articles