Permalink
Browse files

Proguard bootstrap JAR

  • Loading branch information...
alexarchambault committed Nov 30, 2018
1 parent 1637e1f commit e9c31a9a0c3839c3e440a3ee715494ca66c20ee1
@@ -132,13 +132,14 @@ lazy val catsJvm = cats.jvm
lazy val catsJs = cats.js
lazy val bootstrap = project("bootstrap")
.enablePlugins(SbtProguard)
.settings(
pureJava,
dontPublish,
addPathsSources,
// seems not to be automatically found with sbt 0.13.16-M1 :-/
mainClass := Some("coursier.Bootstrap"),
renameMainJar("bootstrap.jar")
proguardedBootstrap
)
lazy val extra = project("extra")
@@ -181,7 +182,7 @@ lazy val cli = project("cli")
shared,
dontPublishIn("2.11"),
coursierPrefix,
unmanagedResources.in(Test) += packageBin.in(bootstrap).in(Compile).value,
unmanagedResources.in(Test) += proguardedJar.in(bootstrap).in(Compile).value,
scalacOptions += "-Ypartial-unification",
libs ++= {
if (scalaBinaryVersion.value == "2.12")
@@ -337,13 +338,15 @@ lazy val addBootstrapJarAsResource = {
import java.nio.file.Files
packageBin.in(Compile) := {
val bootstrapJar = packageBin.in(bootstrap).in(Compile).value
val originalBootstrapJar = packageBin.in(bootstrap).in(Compile).value
val bootstrapJar = proguardedJar.in(bootstrap).in(Compile).value
val source = packageBin.in(Compile).value
val dest = source.getParentFile / (source.getName.stripSuffix(".jar") + "-with-bootstrap.jar")
ZipUtil.addToZip(source, dest, Seq(
"bootstrap.jar" -> Files.readAllBytes(bootstrapJar.toPath)
"bootstrap.jar" -> Files.readAllBytes(bootstrapJar.toPath),
"bootstrap-orig.jar" -> Files.readAllBytes(originalBootstrapJar.toPath)
))
dest
@@ -352,40 +355,39 @@ lazy val addBootstrapJarAsResource = {
lazy val proguardedJarWithBootstrap = Def.task {
import java.nio.charset.StandardCharsets
import java.nio.file.Files
val bootstrapJar = packageBin.in(bootstrap).in(Compile).value
val bootstrapJar = proguardedJar.in(bootstrap).in(Compile).value
val origBootstrapJar = 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")
// 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
ZipUtil.addToZip(source, dest, Seq(
"bootstrap.jar" -> Files.readAllBytes(bootstrapJar.toPath),
"META-INF/MANIFEST.MF" -> manifest.getBytes(StandardCharsets.UTF_8)
"bootstrap-orig.jar" -> Files.readAllBytes(origBootstrapJar.toPath)
))
ZipUtil.addPrelude(dest, dest0)
dest0
}
lazy val proguardedBootstrap = Seq(
proguardedJar := proguardedJarTask.value,
proguardVersion.in(Proguard) := SharedVersions.proguard,
proguardOptions.in(Proguard) ++= Seq(
"-dontwarn",
"-keep class coursier.Bootstrap {\n public static void main(java.lang.String[]);\n}",
"-keep class coursier.IsolatedClassLoader {\n public java.lang.String[] getIsolationTargets();\n}"
),
javaOptions.in(Proguard, proguard) := Seq("-Xmx3172M"),
artifactPath.in(Proguard) := proguardDirectory.in(Proguard).value / "bootstrap.jar"
)
lazy val proguardedCli = Seq(
proguardedJar := proguardedJarTask.value,
proguardVersion.in(Proguard) := SharedVersions.proguard,
proguardOptions.in(Proguard) ++= Seq(
"-dontwarn",
@@ -1,7 +1,7 @@
package coursier
package cli
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, File, FileInputStream, FileOutputStream, IOException}
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, File, FileInputStream, FileOutputStream, IOException, InputStream}
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.Files
@@ -154,15 +154,23 @@ object Bootstrap extends CaseApp[BootstrapOptions] {
javaOpts: Seq[String],
urls: Seq[String],
files: Seq[File],
output: File
output: File,
bootstrapResourcePath: String
): Unit = {
val bootstrapJar =
Option(Thread.currentThread().getContextClassLoader.getResourceAsStream("bootstrap.jar")) match {
case Some(is) => FileUtil.readFully(is)
case None =>
val bootstrapJar = {
var is: InputStream = null
try {
is = Thread.currentThread().getContextClassLoader.getResourceAsStream(bootstrapResourcePath)
if (is == null)
throw new BootstrapException(s"Error: bootstrap JAR not found")
else
FileUtil.readFully(is)
} finally {
if (is != null)
is.close()
}
}
val isolatedDeps = options.options.isolated.isolatedDeps(options.options.common.resolutionOptions.scalaVersion)
@@ -422,7 +430,8 @@ object Bootstrap extends CaseApp[BootstrapOptions] {
javaOpts,
urls,
files,
output0
output0,
if (options.options.proguarded) "bootstrap.jar" else "bootstrap-orig.jar"
)
if (generateBat) {
@@ -47,6 +47,8 @@ final case class BootstrapSpecificOptions(
preamble: Boolean = true,
@Help("Ensure that the output jar is deterministic, set the instant of the added files to Jan 1st 1970")
deterministic: Boolean = false,
@Help("Use proguarded bootstrap")
proguarded: Boolean = true,
@Recurse
isolated: IsolatedLoaderOptions = IsolatedLoaderOptions(),
@Recurse
@@ -1,15 +1,14 @@
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import sbt._
import sbt.Keys._
import sbt.ScriptedPlugin.autoImport.{scriptedBufferLog, scriptedLaunchOpts}
import com.lightbend.sbt.SbtProguard
import com.lightbend.sbt.SbtProguard.autoImport._
import com.typesafe.sbt.pgp._
import coursier.ShadingPlugin.autoImport._
import Aliases._
import ScalaVersion._
@@ -119,17 +118,6 @@ object Settings {
resolvers += "Webjars Bintray" at "https://dl.bintray.com/webjars/maven/"
}
def renameMainJar(name: String) = {
artifactName := {
val artifactName0 = artifactName.value
(sv, m, artifact) =>
if (artifact.`type` == "jar" && artifact.extension == "jar")
name
else
artifactName0(sv, m, artifact)
}
}
lazy val divertThingsPlugin = {
val actualSbtBinaryVersion = Def.setting(
@@ -289,17 +277,45 @@ object Settings {
}
}
lazy val proguardedJar = Def.task {
lazy val proguardedJar = taskKey[File]("")
lazy val proguardedJarTask = Def.task {
val results = proguardTask.value
results match {
case Seq(f) => f
val orig = results match {
case Seq(f0) => f0
case Seq() =>
throw new Exception("Found no proguarded files. Expected one.")
case _ =>
throw new Exception("Found several proguarded files. Don't know how to publish all of them.")
}
val destDir = new File(orig.getParentFile, "with-meta-inf")
destDir.mkdirs()
val dest = new File(destDir, orig.getName)
Files.deleteIfExists(dest.toPath)
// TODO Get from 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
ZipUtil.addToZip(orig, dest, Seq(
"META-INF/MANIFEST.MF" -> manifest.getBytes(StandardCharsets.UTF_8)
))
dest
}
lazy val Integration = config("it").extend(Test)

0 comments on commit e9c31a9

Please sign in to comment.