maven-shade like plugin for SBT

前端 未结 2 1664
梦如初夏
梦如初夏 2021-01-04 10:48

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 fur

相关标签:
2条回答
  • 2021-01-04 11:26

    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/**")
      }
    }
    
    0 讨论(0)
  • 2021-01-04 11:37

    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 讨论(0)
提交回复
热议问题