[Resolved] Merge MANIFEST in existing Jar

Hi,

I have :

  • a jar generated by a A task, with a MANIFEST.MF
  • another MANIFEST.MF generated by a B task (who generate some classes too)

and I wanted to merge the two MANIFEST.MF in a third Jar

I tried something like that first :

task C(type: Jar) { CTask ->
  dependsOn B
  A.finalizedBy CTask

  def outputBDir=...
  def theJar = A.outputs.files.files[0]

  from zipTree(theJar)
  from B.outputs.files.files
  manifest {
    from(zipTree(theJar).find{ it.name=='MANIFEST.MF'}, outputBDir+'/MANIFEST.MF')
  }
  baseName = 'myNewJar'
}

but it fail because zipTree(…).find is evaluated during configuration phase, before theJar creation, and manifest closure checks for file existence…

I finally found an (ugly…) workaround :

  • create a “fake” MANIFEST.MF for avoid error in configuration phase

  • extract the real MANIFEST.MF in the fake in a doFirst

    task C(type: Jar) { CTask ->
    dependsOn B
    A.finalizedBy CTask

    def outputBDir=...
    def theJar = A.outputs.files.files[0]
    
    // create fake MANIFEST.MF
    new File(buildDir.toString()+'/tmp/jar/MANIFEST.MF.tmp').createNewFile() 
    
    from zipTree(theJar)
    from B.outputs.files.files
    manifest {
      // so, manifest closure is happy
      from(buildDir.toString()+'/tmp/jar/MANIFEST.MF.tmp', outputBDir+'/MANIFEST.MF') 
    }
    baseName = 'myNewJar'
    
    // extract the real MANIFEST and overwrite the fake : the merge take the right values....
    doFirst { 
        copy {
            from zipTree(theJar).files.find { it.name == 'MANIFEST.MF' }
            into file(buildDir.toString()+'/tmp/jar')
            rename('MANIFEST.MF', 'MANIFEST.MF.tmp')
        }
    }
    

    }

How can I do this in a better way ?

Use from(Closure) instead of from(files):

manifest {
    from {
        zipTree(theJar).find{ it.name=='MANIFEST.MF'}
    }
    from (outputBDir+'/MANIFEST.MF')
}

It’s work, thanks ;o)