diff --git a/build.sbt b/build.sbt index 7bb560d5a..51ac1cbd4 100644 --- a/build.sbt +++ b/build.sbt @@ -337,15 +337,39 @@ lazy val addBootstrapJarAsResource = { } lazy val addBootstrapInProguardedJar = { + + import java.nio.charset.StandardCharsets + import java.nio.file.Files + ProguardKeys.proguard.in(Proguard) := { val bootstrapJar = packageBin.in(bootstrap).in(Compile).value val source = proguardedJar.value val dest = source.getParentFile / (source.getName.stripSuffix(".jar") + "-with-bootstrap.jar") + val dest0 = source.getParentFile / (source.getName.stripSuffix(".jar") + "-with-bootstrap-and-prelude.jar") - ZipUtil.addToZip(source, dest, Seq("bootstrap.jar" -> bootstrapJar)) + // TODO Get from cli original JAR + val manifest = + s"""Manifest-Version: 1.0 + |Implementation-Title: ${name.value} + |Implementation-Version: ${version.value} + |Specification-Vendor: ${organization.value} + |Specification-Title: ${name.value} + |Implementation-Vendor-Id: ${organization.value} + |Specification-Version: ${version.value} + |Implementation-URL: ${homepage.value.getOrElse("")} + |Implementation-Vendor: ${organization.value} + |Main-Class: ${mainClass.in(Compile).value.getOrElse(sys.error("Main class not found"))} + |""".stripMargin - Seq(dest) + ZipUtil.addToZip(source, dest, Seq( + "bootstrap.jar" -> Files.readAllBytes(bootstrapJar.toPath), + "META-INF/MANIFEST.MF" -> manifest.getBytes(StandardCharsets.UTF_8) + )) + + ZipUtil.addPrelude(dest, dest0) + + Seq(dest0) } } diff --git a/project/ZipUtil.scala b/project/ZipUtil.scala index 73032afb5..3a8f55ab0 100644 --- a/project/ZipUtil.scala +++ b/project/ZipUtil.scala @@ -1,10 +1,14 @@ -import java.util.zip.{ ZipEntry, ZipOutputStream, ZipInputStream } -import java.io.{ ByteArrayOutputStream, FileInputStream, FileOutputStream, File, InputStream, IOException } +import java.util.zip.{ZipEntry, ZipInputStream, ZipOutputStream} +import java.io.{ByteArrayOutputStream, FileInputStream, FileOutputStream, InputStream} +import java.nio.charset.StandardCharsets +import java.nio.file.Files + +import sbt.File object ZipUtil { - def addToZip(sourceZip: File, destZip: File, extra: Seq[(String, File)]): Unit = { + def addToZip(sourceZip: File, destZip: File, extra: Seq[(String, Array[Byte])]): Unit = { val is = new FileInputStream(sourceZip) val os = new FileOutputStream(destZip) @@ -50,9 +54,9 @@ object ZipUtil { outputZip.closeEntry() } - for ((dest, file) <- extra) { + for ((dest, data) <- extra) { outputZip.putNextEntry(new ZipEntry(dest)) - outputZip.write(readFullySync(new FileInputStream(file))) + outputZip.write(data) outputZip.closeEntry() } @@ -63,4 +67,21 @@ object ZipUtil { } + // seems the -noverify may not be needed anymore (to launch the proguarded JAR) + // not sure why + private val prelude = + """#!/usr/bin/env bash + |exec java -noverify -jar "$0" "$@" + |""".stripMargin + + def addPrelude(source: File, dest: File): dest.type = { + + val rawContent = Files.readAllBytes(source.toPath) + val content = prelude.getBytes(StandardCharsets.UTF_8) ++ rawContent + + Files.write(dest.toPath, content) + + dest + } + }