Merging multiple jar files that export services

While developing buildhealth I wanted to create one jar with all the code plus the dependencies.

There are multiple ways to do that, but I was trying to do it the simple way: just unzip the jars, copy all the files to the same place and zip it again (this also made it easy to use proguard on the result jar).

It almost worked (well, if it had worked I wouldn't be writting this, right?), except for the fact that if multiple jars exposed the same services (as defined by the ServiceLoader, which require a file listing the implementing classes) then one of the files would be lost while coping one unzipped jar over another.

To solve that problem I've created a simple ant task that goes over a list of jars and merges the services exported by those. It is called merge-jar-services and should be straightforward to use:

<taskdef resource="org/pescuma/mergeservices/antlib.xml" classpath="lib/merge-services.jar" />

<merge-services dest="${services.dir}">  
    <fileset dir="lib">
        <include name="**/*.jar" />
        <exclude name="**/*-source.jar" />

The sources and the jar are at merge-jar-services (in GitHub). The license is MIT.

PS: If you are using java services, I'd strongly encorage you to use Kohsuke's metainf-services.