I am creating a CLI application in Kotlin with Gradle (Kotlin DSL) as the build system. The CLI will support plugins that will be loaded via ServiceLoader
. In order to do so, the plugins must be in the classpath, and I want users to be able to load their own plugins easily.
I thought about a default directory for the plugin jar files, e.g. $HOME/.my-app/plugins
, and add it to the classpath, so that ServiceLoader
can discover the plugins.
I am planning to compile the application with the application
Gradle plugin. Is there a way to add $HOME/.my-app/plugins
to the classpath in the Gradle build configuration in a platform-independent way? I don't quite know how to do so, since the structure of the home directory depends on the OS.
Am I on the right path? Or is there a simpler way to manage plugin installations?
Seems like a reasonable way to proceed to me.
But loading from a given directory like that is not a Gradle concern. Gradle helps you to build your executable program, but after that the program is in charge.
Instead, from within your program, you can create your own URLClassLoader
pointing at your desired location. Then you can pass your new classloader to the load
method of ServiceLoader
to pick up the service implementations within that location.