Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Akka fat Jar

I need to create a Nutch plugin that communicate with some external applications using Akka. In order to do this, I need to package the plugin as a fat Jar - I am using sbt-assembly version 0.8.3.

When I try to run the plugin, I get the exception

com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka'

as if Akka was not able to find reference.conf. This is weird, because sbt-assembly should be able to package that file correctly, and in fact I can see its content in the created jar.

My build.sbt looks like this:

import AssemblyKeys._

name := "my-project"

version := "0.1-SNAPSHOT"

scalaVersion := "2.10.0"

resolvers ++= Seq(
  "Central Repo" at "http://repo1.maven.org/maven2",
  "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
  "Akka io" at "http://akka.io/repository"
)

libraryDependencies ++= Seq(
  ...,
  "com.typesafe.akka" %% "akka-actor" % "2.1.1",
  "com.typesafe.akka" %% "akka-remote" % "2.1.1"
)

seq(assemblySettings: _*)

mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
  {
    case "plugin.xml" =>
      MergeStrategy.first
    case x if x startsWith "org/apache/jasper" =>
      MergeStrategy.last
    case x if x startsWith "javax/xml" =>
      MergeStrategy.last
    case x if x startsWith "javax/servlet" =>
      MergeStrategy.last
    case x if x startsWith "org/apache/commons" =>
      MergeStrategy.last
    case x if x startsWith "org/apache/xmlcommons" =>
      MergeStrategy.last
    case x if x startsWith "org/xml/sax" =>
      MergeStrategy.last
    case x if x startsWith "org/w3c/dom" =>
      MergeStrategy.last
    case x => old(x)
  }
}

The last lines are needed to fix some conflicts between nutch and hadoop.

What is the correct way to package an Akka application?

like image 548
Andrea Avatar asked Oct 22 '22 15:10

Andrea


1 Answers

The latest version of sbt-assembly as of Nov 2013 is 0.10.1 for sbt 0.13. Using this, I was able to create a fat jar using the latest stable Akka 2.2.3 using the default merge strategy.

Files

project/build.properties:

sbt.version=0.13.0

project/assembly.sbt:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.10.1")

assembly.sbt:

import AssemblyKeys._

assemblySettings

build.sbt:

name := "sbt-assembly-akka-sample"

version := "0.1.0-SNAPSHOT"

scalaVersion := "2.10.3"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.2.3",
  "com.typesafe.akka" %% "akka-remote" % "2.2.3"
)

src/main/scala/actor.scala:

package hello

import akka.actor.{Actor, Props}
import akka.event.Logging

class MyActor extends Actor {
  val log = Logging(context.system, this)
  def receive = {
    case "test" ⇒ log.info("received test")
    case _      ⇒ log.info("received unknown message")
  }
}

src/main/scala/app.scala:

package hello

object Main extends App {
  import akka.actor.{ActorSystem, Props}
  val system = ActorSystem("mySystem")
  val myActor = system.actorOf(Props[MyActor], "myactor")
  myActor ! "test"
}

Output

Here's what I got by running assembly:

> assembly
[info] Updating {file:/xxx/sbt-assembly-01/}sbt-assembly-01...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 2 Scala sources to /xxx/sbt-assembly-01/target/scala-2.10/classes...
[info] Including: uncommons-maths-1.2.2a.jar
[info] Including: protobuf-java-2.4.1.jar
[info] Including: config-1.0.2.jar
[info] Including: netty-3.6.6.Final.jar
[info] Including: akka-remote_2.10-2.2.3.jar
[info] Including: akka-actor_2.10-2.2.3.jar
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for test:test
[info] Including: scala-library-2.10.3.jar
[info] Checking every *.class/*.jar file's SHA-1.
[info] Merging files...
[warn] Merging 'META-INF/NOTICE.txt' with strategy 'rename'
[warn] Merging 'META-INF/license' with strategy 'rename'
[warn] Merging 'META-INF/LICENSE.txt' with strategy 'rename'
[warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard'
[warn] Merging 'reference.conf' with strategy 'concat'
[warn] Strategy 'concat' was applied to a file
[warn] Strategy 'discard' was applied to a file
[warn] Strategy 'rename' was applied to 3 files
[info] SHA-1: 1e9dcebeddc8e2a7d41a0c55a663f9ca0000000
[info] Packaging /xxx/sbt-assembly-01/target/scala-2.10/sbt-assembly-akka-sample-assembly-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[success] Total time: 13 s, completed Nov 11, 2013 8:57:18 PM

And here's what it looks like running the jar:

$ java -jar target/scala-2.10/sbt-assembly-akka-sample-assembly-0.1.0-SNAPSHOT.jar
[INFO] [11/11/2013 20:59:48.265] [mySystem-akka.actor.default-dispatcher-2] [akka://mySystem/user/myactor] received test
like image 200
Eugene Yokota Avatar answered Oct 29 '22 01:10

Eugene Yokota