title: META-INF/services in JAR with Gradle
body: <p>I wanted to build a plugin module that can be loaded with a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html"><code>ServiceLoader</code></a>. This requires adding a file to the <code>META-INF/services</code> directory, that is named after the service interface and that contains the qualifying path to the class that implements it. Then you can load these services by calling <code>ServiceLoader.load()</code>.</p>
  
  <p>Here is an example:</p>
  
  <p>Say we want to provide a plugin interface called <code>org.example.plugins.PluginService</code>. We then provide an implementation of this service in the class <code>org.example.plugins.impl.ExamplePlugin</code>.</p>
  
  <p>If we want to have some sort of plugin mechanism, we could create a JAR file, that contains the implementation. This JAR file must also contain the file <code>META-INF/services/org.example.plugins.PluginService</code>. This file must contain one line</p>
  
  <pre><code>org.example.plugins.impl.ExamplePlugin
  </code></pre>
  
  <p>to enable the <code>ServiceLoader</code> to find the implementation. If that JAR file is in the build path, you can load the plugin by calling</p>
  
  <pre><code>Iterator&lt;PluginService&gt; it = ServiceLoader.load(PluginService.class).iterator();
  </code></pre>
  
  <p>That iterator will give you access too all plugins that are found by the <code>ServiceLoader</code>.</p>
  
  <p>For some reason Gradle doesn't include files into the <code>META-INF</code> directory by default. Is there a way to let the resulting JAR contain such a file?</p>
  
  <p>I already found the method <a href="http://gradle.org/docs/current/groovydoc/org/gradle/api/tasks/bundling/Jar.html#metaInf%28groovy.lang.Closure%29"><code>metaInf</code></a> in class <code>Jar</code>. But I don't know groovy good enough to find the solution on my own.</p>