简体   繁体   中英

maven-shade like plugin for SBT

i'm rellatively new to the scala and sbt world and i'm trying to manage all my new projects with sbt rather than using maven. But now i'm on a point where I dont know further because I can't find any sbt counterpart for the mavnen-shade plugin. What I found only were plugins to include all dependencies, but that is not what I need. So do somebody know a plugin to include certain dependencies into the jar ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM