1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Add MANIFEST.MF at the top of the jar file

This commit is contained in:
Cedric Beust 2018-02-03 10:28:45 -08:00
parent 7a2c4f34da
commit 52f5ceb3d6
2 changed files with 18 additions and 19 deletions

View file

@ -17,7 +17,7 @@ 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. * 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. * 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 { companion object {
val MANIFEST_MF = "META-INF/MANIFEST.MF" val MANIFEST_MF = "META-INF/MANIFEST.MF"
} }
@ -26,6 +26,20 @@ class MetaArchive(outputFile: File, val manifest: Manifest?) : Closeable {
encoding = "UTF-8" 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?) { fun addFile(f: File, entryFile: File, path: String?) {
val file = f.normalize() val file = f.normalize()
FileInputStream(file).use { inputStream -> 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) private fun okToAdd(name: String): Boolean = ! seen.contains(name)
&& ! KFiles.isExcluded(name, DEFAULT_JAR_EXCLUDES) && ! KFiles.isExcluded(name, DEFAULT_JAR_EXCLUDES)
override fun close() { override fun close() = zos.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()
}
private fun addEntry(entry: ArchiveEntry, inputStream: FileInputStream) { private fun addEntry(entry: ArchiveEntry, inputStream: FileInputStream) {
zos.putArchiveEntry(entry) zos.putArchiveEntry(entry)

View file

@ -90,10 +90,7 @@ class OsgiPlugin @Inject constructor(val configActor: ConfigActor<OsgiConfig>, v
val fileSystem = FileSystems.newFileSystem(toFile, null) val fileSystem = FileSystems.newFileSystem(toFile, null)
fileSystem.use { fs -> fileSystem.use { fs ->
JarFile(jarFile).use { jf -> JarFile(jarFile).use { jf ->
val mf = jf.getEntry(MetaArchive.MANIFEST_MF) val mf = jf.getEntry(MetaArchive.MANIFEST_MF) ?: Files.createDirectories(fs.getPath("META-INF/"))
if (mf == null) {
Files.createDirectories(fs.getPath("META-INF/"))
}
val jarManifest = fs.getPath(MetaArchive.MANIFEST_MF) val jarManifest = fs.getPath(MetaArchive.MANIFEST_MF)
Files.write(jarManifest, listOf(lines2), Files.write(jarManifest, listOf(lines2),
if (mf != null) StandardOpenOption.APPEND else StandardOpenOption.CREATE) if (mf != null) StandardOpenOption.APPEND else StandardOpenOption.CREATE)