【问题标题】:maven-shade like plugin for SBT用于 SBT 的 maven-shade 插件
【发布时间】:2015-02-05 10:17:31
【问题描述】:

我对 scala 和 sbt 世界比较陌生,我正在尝试使用 sbt 而不是使用 maven 来管理我的所有新项目。但现在我处于一个我不知道的点,因为我找不到任何 sbt 对应的 mavnen-shade 插件。我发现的只是包含所有依赖项的插件,但这不是我需要的。那么有人知道将某些依赖项包含到 jar 中的插件吗?

【问题讨论】:

  • 我从 2.7 开始编写 Scala,但我仍然更喜欢使用 Maven,FWIW。
  • sbt-assembly plugin 做你想做的事吗?
  • 不完全是,它将 jar 中的所有文件重命名为“OrginalClassName-1.0-SNAPSHOT.class”,因此类加载器找不到它们。
  • 如果你问我认为你在问什么,我不敢相信这只有 1 票。

标签: scala sbt maven-shade-plugin


【解决方案1】:

sbt-assembly 0.14.0 添加了shading 支持。

sbt-assembly 可以隐藏项目或库依赖项中的类。在 Jar Jar Links 的支持下,字节码转换(通过 ASM)用于更改对重命名类的引用。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 2015-08-20
      • 2016-03-08
      • 2017-08-27
      • 1970-01-01
      • 2011-04-16
      • 2021-12-13
      • 2012-05-22
      • 1970-01-01
      相关资源
      最近更新 更多