Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Gradle's Manifest API to insert a `MANIFEST.MF` file into the root of a zip file

We use the manifest attribute of the java plugin to write MANIFEST.MF files to our jar artifacts.

We also use gradle to build GWT projects and the output we've defined for those projects is a zip. I'd like to include a MANIFEST.MF file in the root of that zip file.

I've tried using a task of type: Jar so I can use its manifest property but the problem, of course, is that the manifest file is written into META-INF/MANIFEST.MF, which I don't want. The reason is that we unzip the archive into the main app and I need to be able to reference the MANIFEST.MF file at runtime for my own nefarious purposes.

So right now the archive looks like this:

/gwtdirectory/
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

And I need it took like this:

/gwtdirectory/
/gwtdirectory/MANIFEST.MF
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

I've already successfully gotten it to look like:

/gwtdirectory/
/gwtdirectory/META-INF/MANIFEST.MF
/gwtdirectory/file1
/gwtdirectory/file2
/gwtdirectory/...
/gwtdirectory/filen

Through a definition like:

task pack(type: Jar){
  manifest {
    attributes(...)
  }
  extension = 'zip'
  from gwt.destinationDir
}

The writeTo method looks mighty promising, except I can't find an implementation of that interface that I can use and I'm trying to avoid writing my own.

Thoughts?

like image 609
Tim Visher Avatar asked Jan 23 '12 15:01

Tim Visher


1 Answers

Looking at the Jar task, it does not look like the location of the manifest file is configurable.

You may want to use gradle's own DefaultManifest implementation to create a manifest and then include this file with from in your zip file.

You can try this:

import org.gradle.api.java.archives.internal.DefaultManifest
import org.gradle.api.internal.file.IdentityFileResolver

File manifestFile = file('build/myAwesomeManifest.mf')

task generateManifest << {
   manifest = new DefaultManifest(new IdentityFileResolver())

   // add some attributes
   manifest.attributes(["attr1":"value1", "attr2":"value2"])

   // write it to a file
   manifest.writeTo(manifestFile)
}
like image 103
c_maker Avatar answered Oct 18 '22 10:10

c_maker