Search code examples
eclipsemaventomcatserviceloader

Service Loader config file doesn't explode properly


So I am writing a webapp in Eclipse and I want to use the serviceloader in one of my classes. Question is where to put the META-INF/services stuff. From here (https://stackoverflow.com/a/3421191/2742995) I found:

But the ideal way is to have it in your plugin's jar file. E.g if you have a plugin bundled as WEB-INF/lib/myplugin.jar, and your plugin class is com.example.plugin.MyPlugin Then the jar should have a structure:

myplugin.jar!/META-INF/services/com.example.plugin.MyPlugin

So I have in the module containing the serviceloader stuff, the source: src/main/java/ containing

  1. vcs.validation.* (containing the source code)
  2. a folder: META-INF/services/vcs.validation.javatests.JavaTest containing:

    • Test1 (which reads vcs.validation.javatests.Test1) and
    • Test2 (which reads vcs.validation.javatests.Test2)

(The interface vcs.validation.javatests.JavaTest has two implementing classes Test1 and Test2)

However, when I package the whole webapp as a war and deploy in tomcat the web-app/WEB-INF/classes/ folder does not contain any META-INF/services/. What am I doing wrong here?


Solution

  • Structure should be:

    Project
    | Module
    | | src 
    |   | main 
    |     | java 
    |       | [ source code]      
    |     | resources 
    |       | META-INF 
    |         | services 
    |           | [service files] 
    

    instead of:

    Project
    | Module
    | | src 
    |   | main 
    |     | java 
    |       | [source code]
    |       | META-INF 
    |         | services 
    |           | [service files] 
    

    In this way the service files are no longer exploded to webapp/WEB-INF/classes/META-INF/services but just live in the jar in which they are packaged according to:

    myplugin.jar!/META-INF/services/com.example.plugin.MyPlugin