scala 使用 Akka 构建 sbt 需要什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16701530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:18:55  来源:igfitidea点击:

What's required to have sbt build with Akka?

scalasbtakka

提问by CruncherBigData

I am trying to use scala -akka from sbt.

我正在尝试使用 sbt 的 scala -akka。

My sbt file looks as follows:

我的 sbt 文件如下所示:

name := "hello"

version := "1.0"

scalaVersion := "2.9.1"

resolvers += "akka" at "http://repo.akka.io/snapshots"

libraryDependencies ++= Seq(
  "com.codahale"      % "simplespec_2.9.0-1" % "0.4.1",
  "com.typesafe.akka" % "akka-stm"           % "2.0-SNAPSHOT" 
)

my code:

我的代码:

import akka._

object HelloWorld {
  def main(args: Array[String]) {
    println("Hello, world!")
  }
}

When I do sbt compile, I get

当我这样做时sbt compile,我得到

]# **sbt compile**
[info] Set current project to default-91c48b (in build file:/var/storage1/home/test_user/dev_scala/hello/)
[info] Compiling 1 Scala source to /var/storage1/home/test_user/dev_scala/hello/target/scala-2.9.2/classes...
[error] /var/storage1/home/test_user/dev_scala/hello/src/main/scala/hw.scala:3: not found: object akka
[error] import akka._
[error]        ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 3 s, completed May 22, 2013 8:59:08 PM

Please advice.

请指教。

EDIT2: based on comments bellow. here is the new sbt file

EDIT2:基于下面的评论。这是新的 sbt 文件

name := "hello"
version := "1.0"
scalaVersion := "2.9.1"

resolvers += "akka" at "http://repo.akka.io/snapshots"

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.1.4",
  "com.codahale" % "simplespec_2.9.0-1" % "0.4.1",
  "com.typesafe.akka" % "akka-stm" % "2.0-SNAPSHOT" ,
  "com.typesafe.akka" %% "akka-actor"    % "2.2-M3",
"com.typesafe.akka" %% "akka-slf4j"    % "2.2-M3",
"com.typesafe.akka" %% "akka-remote"   % "2.2-M3",
"com.typesafe.akka" %% "akka-testkit"  % "2.2-M3"% "test"
)

any ideas ?

有任何想法吗 ?

回答by 4lex1v

You didn't have all right dependencies for your project.

您的项目没有所有正确的依赖项。

You have add this one "com.typesafe.akka" %% "akka-actor" % "2.0.5". This one is the main dependency with the core modules for akka. Also it's better to add the following ones for your akka project:

你已经添加了这个"com.typesafe.akka" %% "akka-actor" % "2.0.5"。这是 akka 核心模块的主要依赖项。此外,最好为您的 akka 项目添加以下内容:

"com.typesafe.akka" %% "akka-actor"    % "2.0.5",
"com.typesafe.akka" %% "akka-slf4j"    % "2.0.5",
"com.typesafe.akka" %% "akka-remote"   % "2.0.5",
"com.typesafe.akka" %% "akka-agent"    % "2.0.5", 
"com.typesafe.akka" %% "akka-testkit"  % "2.0.5"% "test"

And to use actors you should import akka.actor._

要使用演员,您应该导入 akka.actor._

Updated

更新

Ok, this build file works for me

好的,这个构建文件对我有用

name := "hello"

version := "1.0"

scalaVersion := "2.10.1"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor"   % "2.2-M3",
  "com.typesafe.akka" %% "akka-slf4j"   % "2.2-M3",
  "com.typesafe.akka" %% "akka-remote"  % "2.2-M3",
  "com.typesafe.akka" %% "akka-agent"   % "2.2-M3",
  "com.typesafe.akka" %% "akka-testkit" % "2.2-M3" % "test"
)

Don't forget to reloadand updateyour project in sbt

不要忘记reloadupdate你的项目在 sbt

回答by JasonG

Your akka-actor dependency absolutely CAN NOT be a different version than your other dependencies. And any dependencies you add absolutely CAN NOT be reliant on different versions of akka either or you'll have a very messed up dependency tree.

您的 akka-actor 依赖项绝对不能是与其他依赖项不同的版本。并且您添加的任何依赖项绝对不能依赖于不同版本的 akka,否则您将拥有一个非常混乱的依赖项树。

And you may as well use current versions if you're starting out. Coltrane 2.2-M3 is current at time of writing.

如果您刚开始,您也可以使用当前版本。Coltrane 2.2-M3 在撰写本文时是最新的。

You can add some more akka libs as needed... But this is a basic starting point based on a real project that we run in prod:

您可以根据需要添加更多 akka 库...但这是基于我们在 prod 中运行的真实项目的基本起点:

name := "app"

organization := "com.yourorg"

version := "0.0.1-SNAPSHOT"

scalaVersion := "2.10.1"

scalacOptions ++= Seq("-unchecked", "-deprecation")

resolvers ++= Seq(
    "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
)


libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.2-M3",
  "com.typesafe.akka" %% "akka-slf4j" % "2.2-M3",
  "com.typesafe.akka" %% "akka-testkit" % "2.2-M3"
)

回答by snukone

UPDATED ON 24.03.2019

2019 年 3 月 24 日更新

To use Akka Actors you need the following dependencies:

要使用 Akka Actors,您需要以下依赖项:

For sbt:

对于 sbt:

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.5.21",
  "com.typesafe.akka" %% "akka-testkit" % "2.5.21" % Test
)

For Gradle:

对于摇篮:

dependencies {
  compile group: 'com.typesafe.akka', name: 'akka-actor_2.12', version: '2.5.21'
  testCompile group: 'com.typesafe.akka', name: 'akka-testkit_2.12', version: '2.5.21'
}

For Maven:

对于 Maven:

<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactId>akka-actor_2.12</artifactId>
  <version>2.5.21</version>
</dependency>
<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactId>akka-testkit_2.12</artifactId>
  <version>2.5.21</version>
  <scope>test</scope>
</dependency>

For more Informations and Dependency-Codesnippets regarding Akka Cluster, Akka Streams, Akka Http, Alpakka etc check: https://akka.io/docs/

有关 Akka Cluster、Akka Streams、Akka Http、Alpakka 等的更多信息和依赖代码片段,请查看:https://akka.io/docs/