6

我需要创建一个使用 Akka 与一些外部应用程序通信的 Nutch 插件。为了做到这一点,我需要将插件打包为一个胖 Jar - 我使用的是sbt-assembly版本 0.8.3。

当我尝试运行插件时,出现异常

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

好像阿卡找不到一样reference.conf。这很奇怪,因为sbt-assembly 应该能够正确打包该文件,实际上我可以在创建的 jar 中看到它的内容。

我的build.sbt样子是这样的:

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)
  }
}

需要最后几行来解决 nutch 和 hadoop 之间的一些冲突。

打包 Akka 应用程序的正确方法是什么?

4

1 回答 1

3

截至 2013 年 11 月, sbt-assembly的最新版本是 sbt 0.13 的0.10.1。使用它,我能够使用默认的合并策略使用最新的稳定 Akka 2.2.3 创建一个胖 jar。

文件

项目/build.properties:

sbt.version=0.13.0

项目/组装.sbt:

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

组装.sbt:

import AssemblyKeys._

assemblySettings

构建.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"
}

输出

这是我通过运行得到的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

下面是运行 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
于 2013-11-12T02:00:36.590 回答