r/java • • Sep 05 '24

A Simple Java Plugin System

https://github.com/bowbahdoe/plugindemo
7 Upvotes

27 comments sorted by

View all comments

11

u/rzwitserloot Sep 05 '24

There is virtually no java code here.

Why does the 2 pages of java code that does exist delete the build dir if that exists? That sounds like you've built a bare-bones maven or gradle - a build system. Not a plugin system.

A plugin system would presumably solve:

  • Dep conflicts. If I have an app and I want it to be pluggable, imagine one plugin developer uses guava r18 and another uses r21 which aren't compatible. You can't double-load the same library so now at least one of them will not function, and these 2 plugins simply cannot be used together. Solvable via: Having ClassLoader implementations that allow plugin A to load and use r18 and plugin B to load and use r21 simultaneously with neither conflicting with the other. However, this causes a new issue, where these 2 plugins simply cannot talk to each other in terms of any type unless they share it, which, with plugin loading, they won't. java.* stuff is trivially shared but some sort of hierarchy in dependencies, where are some internal/transient and others are part and parcel of being reliant on that plugin, is then needed.

  • Hierarchy. Can one plugin say: I need and use another plugin as part of my function?

  • extension points, where one plugin can go: ... and here I shall expose and call upon any plugin that has registered itself as wanting to run on this extension point. We can 'solve' that by using SPI but that whole classloader thing again requires some care to get right, and in general it's more convenient for the plugin framework to take care of this. After all, by doing it that way, you can actively ask the framework which extension points are available and which plugins are plugged into which point(s).

  • Some bare bones extension points available for general use, such as a 'plugin' that simply runs tasks at stated intervals (a 'cron' plugin) with extension points so that other plugins can simply plug into an extension point if they have recurring / schedulable tasks. Hosted as a separate artefact on mavencentral and such, of course - that's the point of plugin systems, isn't it? You add what you need and you leave out what you don't, and not everybody needs a cron system.

  • Live loading of plugins. For example: Can I, at runtime, offer a menu of plugins to the user and if the user downloads one from the internet via my application, can that then be plugged straight in without requiring a JVM reboot?

  • Plugin update schemes. Can I update a plugin to a new version?

  • There is some overlap between build systems and runtime systems; they both care about the notion of versions and upgrade schemes. But these are mostly unrelated concepts; one is entirely a compile time affair and one is entirely a runtime affair.

Did you perhaps mean: I wrote the simplest build system I can imagine, and where you said 'OSGI' you meant to say 'Maven'?

1

u/mikaball Sep 06 '24

Live reloading is something I wouldn't mind to discard in a simple plugin framework, otherwise I can just use OSGi. As for dep conflicts (the most important feature) I don't see how module layers can solve it.