Created
June 3, 2018 18:27
-
-
Save bitsnaps/29f95e962ea610fa28ba83143ae3da46 to your computer and use it in GitHub Desktop.
Create a Jar file programatically using Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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