Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Created June 3, 2018 18:27
Show Gist options
  • Select an option

  • Save bitsnaps/29f95e962ea610fa28ba83143ae3da46 to your computer and use it in GitHub Desktop.

Select an option

Save bitsnaps/29f95e962ea610fa28ba83143ae3da46 to your computer and use it in GitHub Desktop.
Create a Jar file programatically using Groovy
import java.util.jar.*
def outputFile = "MyLib.jar"
def distDir = "path/to/content/MyLib"
/*Manifest manifest = new Manifest()
manifest.mainAttributes[Attributes.Name.MANIFEST_VERSION] = "1.0"*/
def jar = new JarOutputStream(new File("${new File(distDir).parent}/${outputFile}").newOutputStream()/*, manifest*/)
new File(distDir).eachFileRecurse {
addFile(it, jar, distDir)
}
jar.close()
def addFile(File file, JarOutputStream jar, distDir){
def name = file.path.replace('\\', '/').replace(distDir+'/', '')
if (file.isDirectory())
if (!name.endsWith('/'))
name += '/'
def entry = new JarEntry(name)
entry.setTime(file.lastModified())
jar.putNextEntry(entry)
jar.closeEntry()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment