我使用 sbt-proguard 插件在 Proguard 上取得了成功。我花了一段时间来设置它,我不得不关闭一些 Proguard 功能才能让它工作,但最后我得到了我想要的:一个我可以用“java -jar”执行的单个 jar,即使在没有安装 scala 的系统上。
这是我的project/plugins.sbt 启用插件:
resolvers += Resolver.url("sbt-plugin-releases-scalasbt", url("http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/"))(Resolver.ivyStylePatterns)
addSbtPlugin("com.typesafe.sbt" % "sbt-proguard" % "0.2.2")
这里有一些来自我的 build.sbt 的 sn-ps 来配置它:
scalaVersion := "2.10.2"
proguardSettings
ProguardKeys.options in Proguard += ProguardOptions.keepMain("io.package.my.app.Main")
ProguardKeys.options in Proguard ++= Seq(
"-keep class com.sun.xml.wss.impl.misc.XWSSProcessorFactory2_0Impl { *; }", // created dynamically by XWSSProcessorFactory
//
"-dontshrink",
"-dontobfuscate",
"-dontoptimize",
//
// Don't warn is necessary to avoid ProGuard refusing to build the jar.
//
"-dontwarn com.sun.**",
"-dontwarn org.apache.**",
"-dontwarn scala.**",
//
// Don't note just reduces clutter in the build output. If you make changes
// to the ProGuard configuration, you might want to remove these temporarily to
// help debug the new configuration until it's working correctly.
//
"-dontnote com.sun.**",
"-dontnote org.apache.**",
"-dontnote scala.**"
)
//"-printconfiguration /tmp/proguard"
// Examples of how to filter classes.
ProguardKeys.inputFilter in Proguard := { file =>
file.name match {
case "classes" => None
case "org.apache.karaf.shell.console-2.3.2.jar" => Some("org/apache/karaf/shell/**,org/apache/felix/gogo/commands/**")
case "jline-2.9.jar" => Some("jline/**")
case "org.apache.karaf.jaas.modules-2.3.2.jar" => Some("org/apache/karaf/jaas/modules/**")
case "org.apache.karaf.jaas.config-2.3.2.jar" => Some("org/apache/karaf/jaas/config/**")
case "org.osgi.compendium-4.3.1.jar" => Some("!**")
case _ => Some("!META-INF/**")
}
}