28

我正在尝试使用sbt-assembly将 scala 项目转换为可部署的 fat jar 。当我在 sbt 中运行我的组装任务时,我收到以下错误:

Merging 'org/apache/commons/logging/impl/SimpleLog.class' with strategy 'deduplicate'
    :assembly: deduplicate: different file contents found in the following:
    [error] /Users/home/.ivy2/cache/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar:org/apache/commons/logging/impl/SimpleLog.class
    [error] /Users/home/.ivy2/cache/org.slf4j/jcl-over-slf4j/jars/jcl-over-slf4j-1.6.4.jar:org/apache/commons/logging/impl/SimpleLog.class

现在来自 sbt-assembly 文档:

如果多个文件共享相同的相对路径(例如,多个依赖 JAR 中名为 application.conf 的资源),默认策略是验证所有候选文件是否具有相同的内容,否则会出错。可以使用以下内置策略之一或编写自定义策略基于每个路径配置此行为:

  • MergeStrategy.deduplicate是上面描述的默认值
  • MergeStrategy.first按类路径顺序选择第一个匹配的文件
  • MergeStrategy.last选择最后一个
  • MergeStrategy.singleOrError使用有关冲突的错误消息退出
  • MergeStrategy.concat简单地连接所有匹配的文件并包含结果
  • MergeStrategy.filterDistinctLines也连接,但沿途遗漏重复项
  • MergeStrategy.rename重命名源自 jar 文件的文件
  • MergeStrategy.discard简单地丢弃匹配的文件

通过这个我设置我的 build.sbt 如下:

import sbt._
import Keys._
import sbtassembly.Plugin._
import AssemblyKeys._
name := "my-project"
version := "0.1"
scalaVersion := "2.9.2"
crossScalaVersions := Seq("2.9.1","2.9.2")

//assemblySettings
seq(assemblySettings: _*)

resolvers ++= Seq(
    "Typesafe Releases Repository" at "http://repo.typesafe.com/typesafe/releases/",
    "Typesafe Snapshots Repository" at "http://repo.typesafe.com/typesafe/snapshots/",
    "Sonatype Repository" at "http://oss.sonatype.org/content/repositories/releases/"
)

libraryDependencies ++= Seq(
    "org.scalatest" %% "scalatest" % "1.6.1" % "test",
    "org.clapper" %% "grizzled-slf4j" % "0.6.10",
    "org.scalaz" % "scalaz-core_2.9.2" % "7.0.0-M7",
    "net.databinder.dispatch" %% "dispatch-core" % "0.9.5"
)

scalacOptions += "-deprecation"
mainClass in assembly := Some("com.my.main.class")
test in assembly := {}
mergeStrategy in assembly := mergeStrategy.first

在 build.sbt 的最后一行,我有:

mergeStrategy in assembly := mergeStrategy.first

现在,当我运行 SBT 时,我收到以下错误:

error: value first is not a member of sbt.SettingKey[String => sbtassembly.Plugin.MergeStrategy]
    mergeStrategy in assembly := mergeStrategy.first

有人可以指出我在这里可能做错了什么吗?

谢谢

4

7 回答 7

14

对于当前版本0.11.2(2014-03-25),定义合并策略的方式不同。

在此处记录,相关部分是:

注意:程序集中的 mergeStrategy 需要一个函数,你不能这样做

mergeStrategy in assembly := MergeStrategy.first

新方法是(从同一来源复制):

mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
  {
    case PathList("javax", "servlet", xs @ _*)         => MergeStrategy.first
    case PathList(ps @ _*) if ps.last endsWith ".html" => MergeStrategy.first
    case "application.conf" => MergeStrategy.concat
    case "unwanted.txt"     => MergeStrategy.discard
    case x => old(x)
  }
}

这可能也适用于早期版本,我不知道它是什么时候改变的。

于 2014-03-25T15:28:32.440 回答
10

我觉得应该是MergeStrategy.first带资本的M,所以mergeStrategy in assembly := MergeStrategy.first

于 2013-02-10T11:46:27.080 回答
9

这是合并大多数常见 java/scala 项目的正确方法。它负责 META-INF 和类。

还负责 META-INF 中的服务注册。

assemblyMergeStrategy in assembly := {
case x if Assembly.isConfigFile(x) =>
  MergeStrategy.concat
case PathList(ps @ _*) if Assembly.isReadme(ps.last) || Assembly.isLicenseFile(ps.last) =>
  MergeStrategy.rename
case PathList("META-INF", xs @ _*) =>
  (xs map {_.toLowerCase}) match {
    case ("manifest.mf" :: Nil) | ("index.list" :: Nil) | ("dependencies" :: Nil) =>
      MergeStrategy.discard
    case ps @ (x :: xs) if ps.last.endsWith(".sf") || ps.last.endsWith(".dsa") =>
      MergeStrategy.discard
    case "plexus" :: xs =>
      MergeStrategy.discard
    case "services" :: xs =>
      MergeStrategy.filterDistinctLines
    case ("spring.schemas" :: Nil) | ("spring.handlers" :: Nil) =>
      MergeStrategy.filterDistinctLines
    case _ => MergeStrategy.first
  }
case _ => MergeStrategy.first}
于 2017-01-04T22:06:31.727 回答
8

我刚刚设置了一个需要重新连接一些 mergeStrategies 的小 sbt 项目,发现答案有点过时,让我添加我的版本工作代码(截至 2015 年 4 月 7 日)

  • sbt 0.13.8
  • 斯卡拉 2.11.6
  • 组装 0.13.0

    mergeStrategy in assembly := {
      case x if x.startsWith("META-INF") => MergeStrategy.discard // Bumf
      case x if x.endsWith(".html") => MergeStrategy.discard // More bumf
      case x if x.contains("slf4j-api") => MergeStrategy.last
      case x if x.contains("org/cyberneko/html") => MergeStrategy.first
      case PathList("com", "esotericsoftware", xs@_ *) => MergeStrategy.last // For Log$Logger.class
      case x =>
         val oldStrategy = (mergeStrategy in assembly).value
         oldStrategy(x)
    }
    
于 2015-07-04T09:21:38.407 回答
4

对于新的 sbt 版本(sbt-version :0.13.11),我收到了 slf4j 的错误;暂时采取简单的方法:还请在此处检查答案Scala SBT Assembly cannot merge due to deduplication error in StaticLoggerBinder.class 其中提到了sbt-dependency-graph工具,手动执行此操作非常酷

assemblyMergeStrategy in assembly <<= (assemblyMergeStrategy in assembly) {
  (old) => {
    case PathList("META-INF", xs @ _*) => MergeStrategy.discard
    case x => MergeStrategy.first
  }
}
于 2016-07-15T10:53:26.573 回答
1

快速更新:mergeStrategy 已弃用。使用 assemblyMergeStrategy。除此之外,早期的反应仍然可靠

于 2019-02-22T18:01:58.980 回答
0

将以下内容添加到 build.sbt 以将 kafka 添加为源或目标

 assemblyMergeStrategy in assembly := {
 case PathList("META-INF", xs @ _*) => MergeStrategy.discard
 //To add Kafka as source
 case "META-INF/services/org.apache.spark.sql.sources.DataSourceRegister" => 
 MergeStrategy.concat
 case x => MergeStrategy.first
 }
于 2019-09-20T07:48:27.643 回答