play 2.4 console not working as documented

2020-02-26 12:39发布

问题:

i have recently updated to play 2.4.1 (damiya) release and have always been able to enter the scala console both through intellij and by entering activator console in my terminal window. I would then start a new static application by entering this line of code:

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

as documented on the play website itself as well as on some answers to similar older questions on SO.

however, i am unable to get this to work on play 2.4.1, with the error being returned as :

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

any advice on how to fix this would be greatly appreciated, the console was immensely useful to me in the past and rather essential for debugging purposes.

回答1:

Richard explains in this commit:

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.

You can do the same thing as follows:

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


回答2:

Unfortunately, bjfletcher's answer only pointed me on the right path-- running ProdServerStart didn't actually get me a running environment (in fact, quite the opposite--

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

...probably due to my own inexperience with the JVM.).

Fortunately, the 2.5.x version of Launch the Interactive Console provides runnable code!

The more verbose version of what we had before is now:

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 

which can probably be saved into a :script or something.