From 52f5ceb3d66769e239104a62f76315cbadafd0aa Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sat, 3 Feb 2018 10:28:45 -0800 Subject: [PATCH] Add MANIFEST.MF at the top of the jar file --- .../com/beust/kobalt/archive/MetaArchive.kt | 32 ++++++++++--------- .../beust/kobalt/plugin/osgi/OsgiPlugin.kt | 5 +-- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt index ddb1e727..29f05a43 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/archive/MetaArchive.kt @@ -17,15 +17,29 @@ import org.apache.commons.compress.archivers.zip.ZipFile as ApacheZipFile * Abstraction of a zip/jar/war archive that automatically manages the addition of expanded jar files. * Uses ZipArchiveOutputStream for fast inclusion of expanded jar files. */ -class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable { +class MetaArchive(private val outputFile: File, val manifest: Manifest?) : Closeable { companion object { val MANIFEST_MF = "META-INF/MANIFEST.MF" } - private val zos = ZipArchiveOutputStream(outputFile).apply { + private val zos= ZipArchiveOutputStream(outputFile).apply { encoding = "UTF-8" } + init { + // If no manifest was passed, create an empty one so it's the first one in the archive + val m = manifest ?: Manifest() + Files.createTempFile("kobalt", "tmpManifest").toFile().let { manifestFile -> + FileOutputStream(manifestFile).use { fos -> + m.write(fos) + } + + val entry = zos.createArchiveEntry(manifestFile, MetaArchive.MANIFEST_MF) + addEntry(entry, FileInputStream(manifestFile)) + } + } + + fun addFile(f: File, entryFile: File, path: String?) { val file = f.normalize() FileInputStream(file).use { inputStream -> @@ -57,19 +71,7 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable { private fun okToAdd(name: String): Boolean = ! seen.contains(name) && ! KFiles.isExcluded(name, DEFAULT_JAR_EXCLUDES) - override fun close() { - if (manifest != null) { - Files.createTempFile("aaa", "bbb").toFile().let { manifestFile -> - FileOutputStream(manifestFile).use { fos -> - manifest.write(fos) - } - - val entry = zos.createArchiveEntry(manifestFile, MetaArchive.MANIFEST_MF) - addEntry(entry, FileInputStream(manifestFile)) - } - } - zos.close() - } + override fun close() = zos.close() private fun addEntry(entry: ArchiveEntry, inputStream: FileInputStream) { zos.putArchiveEntry(entry) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/osgi/OsgiPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/osgi/OsgiPlugin.kt index 83ae5228..8a2a06b8 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/osgi/OsgiPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/osgi/OsgiPlugin.kt @@ -90,10 +90,7 @@ class OsgiPlugin @Inject constructor(val configActor: ConfigActor, v val fileSystem = FileSystems.newFileSystem(toFile, null) fileSystem.use { fs -> JarFile(jarFile).use { jf -> - val mf = jf.getEntry(MetaArchive.MANIFEST_MF) - if (mf == null) { - Files.createDirectories(fs.getPath("META-INF/")) - } + val mf = jf.getEntry(MetaArchive.MANIFEST_MF) ?: Files.createDirectories(fs.getPath("META-INF/")) val jarManifest = fs.getPath(MetaArchive.MANIFEST_MF) Files.write(jarManifest, listOf(lines2), if (mf != null) StandardOpenOption.APPEND else StandardOpenOption.CREATE)