Skip to content

Instantly share code, notes, and snippets.

@j-keck
Last active February 26, 2018 11:11
Show Gist options
  • Save j-keck/d15d768ca8392574035ca012ec1fe434 to your computer and use it in GitHub Desktop.
Save j-keck/d15d768ca8392574035ca012ec1fe434 to your computer and use it in GitHub Desktop.
import sbt._
import sbt.Keys._
import sbtassembly.AssemblyPlugin, AssemblyPlugin.autoImport._
object ProguardKeys {
val proguard = taskKey[File]("builds the proguarded jar file")
val proguardOptions = settingKey[Seq[String]]("CLI options")
val proguardVersion = settingKey[String]("proguard's artefact version")
val Proguard: Configuration = config("proguard").hide
}
object ProguardPlugin extends AutoPlugin {
val autoImport = ProguardKeys
import autoImport._
override def requires = AssemblyPlugin
override def trigger = noTrigger
override def projectConfigurations = Seq(Proguard)
override def projectSettings =
Seq(
proguardOptions := Nil,
proguardVersion := "5.3.3",
libraryDependencies += "net.sf.proguard" % "proguard-base" % proguardVersion.value % Proguard,
proguard := {
val app_jars = (managedClasspath in Proguard).value
// not java 9 compatible...
val syslib = file(scala.util.Properties.jdkHome) / "jre/lib/rt.jar"
val options = ForkOptions(
javaHome = javaHome.value,
outputStrategy = outputStrategy.value,
bootJars = Vector.empty[java.io.File],
workingDirectory = Option(baseDirectory.value),
runJVMOptions = (javaOptions in proguard).value.toVector,
connectInput = connectInput.value,
envVars = Map.empty[String, String]
)
FileFunction
.cached(streams.value.cacheDirectory / "proguard") {
(in: Set[File]) =>
val Seq(input) = in.toSeq
val out = target.value / s"${name.value}-proguard-${version.value}.jar"
new ForkRun(options)
.run(
"proguard.ProGuard",
Attributed.data(app_jars),
Seq("-injars", input.getCanonicalPath) ++
Seq("-outjars", out.getCanonicalPath) ++
Seq("-libraryjars", syslib.getCanonicalPath) ++
proguardOptions.value,
state.value.log
)
.failed
.foreach(f => sys.error(f.getMessage))
Set(out)
}(Set(assembly.value))
.head
}
) ++ inConfig(Proguard)(
managedClasspath := Classpaths
.managedJars(configuration.value, classpathTypes.value, update.value)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment