How to include a config file in the "META-INF/services" folder of a JAR using Maven

Matt Vukas picture Matt Vukas · Jul 8, 2013 · Viewed 86.9k times · Source

I'm working on a Java project in Eclipse, which is built using Maven. I'm using some recycled code from an older project, and one of these classes looks for a file in the META-INF/services folder of the JAR with a particular name, then parses the text of this file. In this particular example, it looks for a file with the name of a Java interface, then grabs the class name of the implementation from inside the file.

So basically what I'm trying to do is include a file in the "META-INF/services" folder of the JAR with a file name (X), and one line of text (Y). I'm guessing this should be done using Maven, maybe by specifying an attribute in the POM file, but my research hasn't turned up anything. I know you're not supposed to hard-code or manually type out any META files, so I'm not sure what to do here.

Answer

Brian picture Brian · Jul 8, 2013

Create a new source folder with the location src/main/resources, then create your META-INF/services folder in there and drop in your FQCN file. This should copy them into the jar file automatically. So you'll have:

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

It's worth noting that this applies to Gradle projects with the default source sets as well.