Search code examples
scalasbtannotation-processingserviceloader

Automatic META-INF/services generation in Scala and SBT for ServiceLoader


Is there a way, in Scala and SBT, to automatically generate META-INF/services/* resource files for later use by java.util.ServiceLoader by annotating classes, like Google Auto Service does for Java projects?

i.e.

package foo.bar

import my.exported.ServiceInterface

@AutoService[ServiceInterface]
class MyService extends ServiceInterface{
  // …
}

to automatically generate the file META-INF/services/my.exported.ServiceInterface in the resources folder. The file will contain:

foo.bar.MyService 

(I don't think I can use Google Auto Service directly, as it doesn't work with Scala classes -- see this comment on a realm-java github issue.)


Solution

  • Please consider using https://github.com/nyavro/spi-plugin.

    The approach used in this plugin differs from using annotations - it uses whole packages as a source of interfaces and applies to packages of interface implementations.