0
votes

I am trying to create a jar file for my project. I am using sbt assembly command to generate one.

But getting error when it starts merging files:

scala.MatchError: spray\http\parser\ProtocolParameterRules$$anonfun$DeltaSeconds$1.class (of class java.lang.String)

My build.sbt looks like this:

lazy val commonSettings = Seq(
  name := "SampleSpray",
  version := "1.0",
  scalaVersion := "2.11.7",
  organization := "com.test"

)
mainClass in assembly := Some("com.example.Boot")

lazy val root = (project in file(".")).
  settings(commonSettings: _*).
  settings(
    name := "test",
    resolvers += "spray repo" at "http://repo.spray.io",
    libraryDependencies ++= {
            val akkaV = "2.3.9"
            val sprayV = "1.3.3"
            Seq(
              "io.spray"            %%  "spray-can"     % sprayV,
              "io.spray"            %%  "spray-routing" % sprayV,
              "io.spray"            %%  "spray-json"    % "1.3.2",
              "io.spray"            %%  "spray-testkit" % sprayV  % "test",
              "com.typesafe.akka"   %%  "akka-actor"    % akkaV,
              "com.typesafe.akka"   %%  "akka-testkit"  % akkaV   % "test",
              "org.specs2"          %%  "specs2-core"   % "2.3.11" % "test",
              "com.sksamuel.elastic4s" %% "elastic4s-core" % "2.1.0",
              "com.sksamuel.elastic4s" %% "elastic4s-jackson" % "2.1.0",
              "net.liftweb" %% "lift-json" % "2.6+"
            )
  }
)


assemblyOption in assembly := (assemblyOption in assembly).value.copy(cacheUnzip = false)

assemblyMergeStrategy in assembly := {
  case "BaseDateTime.class"                            => MergeStrategy.first

}

Don't know why the error is coming.

1

1 Answers

0
votes

The setting assemblyMergeStrategy in assembly has the type String => MergeStrategy.

In your sbt file you are using the partial function

{
   case "BaseDateTime.class" => MergeStrategy.first
}

which is syntactic sugar for

(s:String) => {
  s match {
    case "BaseDateTime.class" => MergeStrategy.first
  }
}

This representation shows that the given function will not exhaustively match all passed strings. In your case sbt-assembly tried to merge the file named spray\http\parser\ProtocolParameterRules$$anonfun$DeltaSeconds$1.class into the fat jar, but could not find any matching merge strategy. You need a "default" case also:

(s:String) => {
  s match {
    case "BaseDateTime.class" => MergeStrategy.first
    case x =>
      val oldStrategy = (assemblyMergeStrategy in assembly).value
      oldStrategy(x)
  }
}

Or written as partial function:

{
  case "BaseDateTime.class" => MergeStrategy.first
  case x =>
    val oldStrategy = (assemblyMergeStrategy in assembly).value
    oldStrategy(x)
}

I also ran into the same issue when sbt-assembly's assembly task failed to create a fat jar due to a name conflict in the elasticsearch and its transitive joda-time dependencies. Elasticsearch redefines the class org.joda.time.base.BaseDateTime which is already implemented in the joda-time library. I've followed your approach to tell sbt-assembly how resolve this conflict using the following assemblyMergeStrategy:

assemblyMergeStrategy in assembly := {
  case "org/joda/time/base/BaseDateTime.class" => MergeStrategy.first
  case x =>
    val oldStrategy = (assemblyMergeStrategy in assembly).value
    oldStrategy(x)
}