找不到密钥 akka 的配置设置

no configuration setting found for key akka

我在我的一个项目中使用 scala、spray 和 akka。在 Intellij 中,它运行良好。当我构建项目并尝试在命令行中 运行 它时,出现以下错误。


Caused by: com.typesafe.config.ConfigException$Missing: 没有配置设置
 找到键 'akka'
        在 com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124)
        在 com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:147)
        在 com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)
        在 com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)
        在 com.typesafe.config.impl.SimpleConfig.getString(SimpleConfig.java:206)
        在 akka.actor.ActorSystem$Settings.(ActorSystem.scala:168)
        在 akka.actor.ActorSystemImpl.(ActorSystem.scala:504)
        在 akka.actor.ActorSystem$.apply(ActorSystem.scala:141)
        在 akka.actor.ActorSystem$.apply(ActorSystem.scala:108)
        在 akka.actor.ActorSystem$.apply(ActorSystem.scala:99)

请帮我解决问题

Akka 默认会从以下位置读取配置文件:

  1. application.conf 在类路径的根目录下(包括在 jar 中)
  2. 从 ActorSystem("name", config) 手动传入配置。
  3. reference.conf 在类路径的根目录下(包括在 jar 中)

请仔细检查您的类路径,看看您是否有错误的类路径引用,这表明 akka jar、spray jar 等的类路径根有问题

问题是在使用 sbt:assembly 时,默认合并策略根据

排除所有 reference.conf 文件

如果多个文件共享相同的相对路径(例如,多个依赖 JAR 中名为 application.conf 的资源),默认策略是验证所有候选者是否具有相同的内容并出错否则。

解决方法是添加一个MergeStrategy如下

assemblyMergeStrategy in assembly := {
  case PathList("reference.conf") => MergeStrategy.concat
}

maven 用户的 maven-shade-plugin 配置:

<configuration>
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>reference.conf</resource>
        </transformer>
    </transformers>
</configuration>