How to generate a file for inclusion in a zip archive


(Paul Chown) #1

I am trying to create a jar file, but am using a zip task as the jar file contains no java code. To complete the jar I need to create a manifest and include this into the archive, xomething like the following:

task webjar(type: Zip) {
  extension = 'jar'
  //...
  into("META-INF") {
    from createManifest()
  }
}
  def createManifest() {
  def manifestFile = file(new File(project.buildDir, "MANIFEST.MF"))
  manifestFile.text = "manifestContent"
  Thread.sleep(10000)
  return manifestFile
}

There are two problems:

  • the manifest file created appears in the build directory, but then disappears again (the Thread.sleep() allows you to see this)

  • the manifest file isn’t included in the archive

Does the file have to have a relative path to be included? Is there something that is cleaning up the files in the build directory? Is the archive only being assembled after the file has been deleted?

If I simply use a relative path then it works fine, but leaves the ‘temporary’ manifest file in the source tree, which isn’t good either.


(kellyrob) #2

If the file is disappearing, it might be because you’re invoking something like ‘clean webjar’. Because your createManifest() method is being called during the task configuration phase of the ‘webjar’ zip task, a call to ‘clean’ will delete it(and the entire build directory) during the task execution phase. If you create your manifest file(assuming it needs to be dynamically generated) in a separate task, you can get the behaviour that you’re looking for.

apply plugin: 'base'
def manifestFile = file("$buildDir/MANIFEST.MF")
task createFile(){
 outputs.file(manifestFile)
 doLast{
  buildDir.mkdir()
    manifestFile.text = "manifestContent"
 }
}
  task webjar(type: Zip, dependsOn: createFile) {
  baseName = 'test'
  extension = 'jar'
  into("META-INF") {
    from createFile.outputs
  }
}

(Paul Chown) #3

Thanks for the clarification. In the end I went back to using the java plugin and the standard jar task, as I needed too many workarounds. This information will still be useful, though, as there are some other files that need to be dynamically generated.