Merging two jars into one


(Chryssa Charalambides) #1

I need to merge two jars into one.

Jar A is a shadow jar of my entire project. Jar B is the result of running a code generator that produces a few files.

I want to produce jar C that has all files from jar B, and all files from jar A minus the ones that have the same name as the ones in jar B. In other words, I want to replace the files in jar A with the generated files from jar B.

I have a solution where it includes everything from jar A and jar B but when I try to apply an exclude pattern, it excludes everything from jar A and just leaves me with the contents of jar B only.

This is what it looks like. Why does it exclude everything from jar A

task mergeJars(type: Jar, dependsOn: ['clean', 'shadowJar']) {
  FileTree generatedSolvers = fileTree(dir: 'temp/src')
  FileTree existingSolvers = zipTree("build/libs/jar-all.jar")

  from generatedSolvers
  from ({ existingSolvers }) {
    exclude { details ->
      generatedSolvers.each { generated ->
        def className = generated.getName().substring(0, generated.getName().lastIndexOf("."))
        details.getName().endsWith(className + '.class')
      }
    }
  }
}

(uklance) #2

This sounds a little similar to what I do in my monkey patch plugin. See here where I use an eachFile closure to exclude() a file from a Jar if it has an override