15

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 ?

4
  • 1
    I've been writing Scala since 2.7 and I still prefer to use Maven, FWIW.
    – lmm
    Dec 7, 2014 at 23:31
  • Does the sbt-assembly plugin do what you want?
    – Kulu Limpa
    Dec 8, 2014 at 4:23
  • Not exactly, it renames all files in jar like "OrginalClassName-1.0-SNAPSHOT.class" so the classloader does not find them.
    – shim_
    Dec 8, 2014 at 12:38
  • If your asking what I think your asking, I cannot believe this has only 1 upvote.
    – samthebest
    Dec 26, 2014 at 14:57

2 Answers 2

11

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.