关于 scala:play 2.4 控制台未按记录工作

play 2.4 console not working as documented

我最近更新到播放 2.4.1 (damiya) 版本,并且一直能够通过 intellij 和在我的终端窗口中输入 activator console 进入 scala 控制台。然后我会通过输入这行代码来启动一个新的静态应用程序:

new play.core.StaticApplication(new java.io.File("."))

正如游戏网站本身以及对 SO 上类似旧问题的一些答案所记录的那样。

但是,我无法让它在 play 2.4.1 上运行,错误返回为:

1
2
3
<console>:8: error: type StaticApplication is not a member of package play.core
          new play.core.StaticApplication(new java.io.File("."))
                        ^ `

任何有关如何解决此问题的建议将不胜感激,控制台在过去对我非常有用,并且对于调试目的相当重要。


Richard 在此提交中解释说:

Refactored server start code into prod, dev, test modes

This change makes the lifecycle for starting up applications much
clearer.

  • No longer need separate ServerStart implementations for Netty and Akka HTTP because ServerProvider configuration is always loaded from
    configuration files. Instead, separate out code according to the mode
    that the server runs in, because behavior can vary between modes. Now
    we have a ProdServerStart, DevServerStart and a DocServerStart.
  • For each mode, move the ApplicationProvider code into same file as the new server startup code. Move code for starting up the application
    out of the ApplicationProvider constructors and into the server
    start code. ApplicationProviders still implement the 'get' method
    for getting the current Application.
  • Remove TestApplication and StaticApplication, because they do the same thing. Instead provide helpers for 'static' Applications that
    don't need reloading.

你可以这样做:

1
play.core.server.ProdServerStart.main(Array())


不幸的是,bjfletcher 的回答只为我指明了正确的道路——运行 ProdServerStart 实际上并没有给我一个运行环境(事实上,恰恰相反——

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
scala> play.core.server.ProdServerStart.main(Array.empty)
Oops, cannot start the server.
Configuration error: Configuration error[application: application.conf: java.io.IOException: resource not found on classpath: application.conf, application.json: java.io.IOException: resource not found on classpath: application.json, application.properties: java.io
.IOException: resource not found on classpath: application.properties]
        at play.api.Configuration$.configError(Configuration.scala:178)
        at play.api.Configuration$.load(Configuration.scala:103)
        at play.api.Configuration$.load(Configuration.scala:133)
        at play.api.ApplicationLoader$.createContext(ApplicationLoader.scala:91)
        at play.core.server.ProdServerStart$.start(ProdServerStart.scala:50)
        at play.core.server.ProdServerStart$.main(ProdServerStart.scala:27)
        at $line21.$read$$iw$$iw$.<init>(<console>:8)
        at $line21.$read$$iw$$iw$.<clinit>(<console>)
        at $line21.$eval$.$print$lzycompute(<console>:7)
        at $line21.$eval$.$print(<console>:6)
        at $line21.$eval.$print(<console>)
        ...

...可能是因为我自己对 JVM 缺乏经验。)。

幸运的是,2.5.x 版本的 Launch the Interactive Console 提供了可运行的代码!

我们之前拥有的更详细的版本现在是:

1
2
3
4
5
6
7
import play.api._
val env = Environment(new java.io.File("."), this.getClass.getClassLoader, Mode.Dev)
val context = ApplicationLoader.createContext(env)
val loader = ApplicationLoader(context)
val app = loader.load(context)
Play.start(app)
import Play.current

可能可以保存到 :script 或其他东西中。