maven-shade like plugin for SBT

倖福魔咒の 提交于 2019-12-01 02:53:55

sbt-assembly 0.14.0 adds shading support.

sbt-assembly can shade classes from your projects or from the library dependencies. Backed by Jar Jar Links, bytecode transformation (via ASM) is used to change references to the renamed classes.

assemblyShadeRules in assembly := Seq(
  ShadeRule.rename("org.apache.commons.io.**" -> "shadeio.@1").inAll
)

I've had success with Proguard using the sbt-proguard plugin. It took me a while to get it set up, and I had to turn off some of the Proguard features to get it working, but in the end I got what I wanted: a single jar I could execute with "java -jar", even on a system without scala installed.

Here is my project/plugins.sbt to enable the plugin:

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")

And here are some snippets from my build.sbt to configure it:

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/**")
  }
}

I'm looking for a similar thing as I'm totally fed up with cross-version suffix errors and deduplicate errors from sbt-assembly.

I managed to find "onejar" plugin but it hasn't been updated for an entire year, let me know if works: https://github.com/sbt/sbt-onejar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!