Removing META-INF directory from sbt assembly

I recently had a look into sbt assembly, since I had to create a fat .jar for my project. Unfortunately I couln’t get it running, getting the following error all the time:

[info] Merging files...
java.lang.RuntimeException: deduplicate: different file contents found in the following:

The problem is, that all dependencies usually contain a META-INF directory with e.g. a DEPENDENCIES or a MANIFEST.MF file. So when sbt assembly tries to merge the different jars and finds already existing files, it’s default behaviour is to use a deduplication strategy, which is described as follows1:

If multiple files share the same relative path (e.g. a resource named application.conf in multiple dependency JARs), the default strategy is to verify that all candidates have the same contents and error out otherwise.

So sbt assembly stops with an error. The default strategy should filter it out:

case PathList("META-INF", xs @ _*) =>

However, it somehow doesn’t work. It took me quite some time to figure out how to circumvent this problem, so I want to share the solution with you here:

1 Sbt assembly documentation

Written on March 20, 2014