mirror of https://github.com/sbt/sbt.git
Make the proguarded JAR directly runnable
This commit is contained in:
parent
f43d8d59ad
commit
fa8d952e81
28
build.sbt
28
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue