How to create a fat-jar for a Spring project

I’d like to use Gradle as my build environment for a Java SE project involving Spring DI + Spring ORM. When I create a fat-jar of the program and it’s dependencies the META-INF/spring.schemas are duplicated and contain incomplete information.

What can I do to merge the schemas before adding them to the fat-jar?

Depends on how you create the fat Jar. But if you truly need to merge the schemas (rather than selecting one of them to go into the fat Jar), you’ll have to do it yourself, with plain Groovy.

I’m creating the fat-jar through the mechanism recommended in the Gradle Cookbook (http://wiki.gradle.org/display/GRADLE/Cookbook#Cookbook-Includeallruntimedependencies). e.g.

jar {

dependsOn configurations.runtime

from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } }

If you want to include just some of the schemas, you can do something like this:

jar {
  ...
  from {
     configurations.runtime.collect { file ->
      if (file.directory) return file
      if (shouldIncludeSchemasFrom(file)) return zipTree(file)
      zipTree(file).matching { exclude "META-INF/spring.schemas/" }
      }
  }
}
  boolean shouldIncludeSchemasFrom(File file) { ... }

If you need true merging, you first have to somehow generate the merged files (probably as a separate task), and then include them into the fat Jar while excluding the original files.

That’ll do the trick. Thank you.