com-lihaoyi / Ammonite

Scala Scripting
http://ammonite.io
MIT License
2.6k stars 367 forks source link

Error importing play using documentation code snippet #748

Open gbersac opened 6 years ago

gbersac commented 6 years ago

When I try to import play using the code snippet in the documentation, I get the following error :

Welcome to the Ammonite Repl 1.0.3
(Scala 2.12.4 Java 1.8.0_112)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ import $ivy.{
      `com.typesafe.play::play:2.5.0`,
      `com.typesafe.play::play-netty-server:2.5.0`,
      `org.scalaj::scalaj-http:2.2.1`
    }
Failed to resolve ivy dependencies:
  org.scalaj:scalaj-http_2.12:2.2.1
    not found: /Users/gbe/.ivy2/local/org.scalaj/scalaj-http_2.12/2.2.1/ivys/ivy.xml
    not found: https://repo1.maven.org/maven2/org/scalaj/scalaj-http_2.12/2.2.1/scalaj-http_2.12-2.2.1.pom
  com.typesafe.play:play-netty-server_2.12:2.5.0
    not found: /Users/gbe/.ivy2/local/com.typesafe.play/play-netty-server_2.12/2.5.0/ivys/ivy.xml
    not found: https://repo1.maven.org/maven2/com/typesafe/play/play-netty-server_2.12/2.5.0/play-netty-server_2.12-2.5.0.pom
  com.typesafe.play:play_2.12:2.5.0
    not found: /Users/gbe/.ivy2/local/com.typesafe.play/play_2.12/2.5.0/ivys/ivy.xml
    not found: https://repo1.maven.org/maven2/com/typesafe/play/play_2.12/2.5.0/play_2.12-2.5.0.pom

Now, if I try to impot it using the new dependencies system for scripts :

interp.load.ivy("com.typesafe.play" %% "play" % "2.6.11")
interp.load.ivy("com.typesafe.play" %% "play-akka-http-server" % "2.6.11")
interp.load.ivy("org.scalaj" %% "scalaj-http" % "2.3.0")

@

import play.api.mvc._

Will give the following erro :

$  amm -w play.sc
Compiling /Users/gbe/debug/play.sc
Compiling /Users/gbe/debug/play.sc #2
play.sc:7: value api is not a member of object ammonite.$file.play
import play.api.mvc._
            ^
Compilation Failed

Could you please help me import play in ammonite scripts ? I also think that the documentation should be updated to have a valid (and up to date) Play Framework Server section

eptx commented 6 years ago

Any progress on this?

eptx commented 6 years ago

Tried to get running but after 2hr just don't have more time ...

Tried following https://github.com/lloydmeta/slim-play including using the conf file. However, I could not figure out how to add it to the class path or programmatically configure. Tried system env override of application.conf with this ....

java -Dconfig.file=/Users/me/application.conf -jar $(which amm) --no-remote-logging playtest.sc

...but still can't load the app. Here's the error:

@7802832ib: Cannot load play.application.loader play.utils.Reflect$.loadClass$1(Reflect.scala:111) play.utils.Reflect$.configuredClass(Reflect.scala:124) play.api.ApplicationLoader$.apply(ApplicationLoader.scala:73) ammonite.$file.playtest$.(playtest.sc:65) ammonite.$file.playtest$.(playtest.sc) java.lang.ClassNotFoundException: AppLoader java.net.URLClassLoader.findClass(URLClassLoader.java:381) ammonite.runtime.SpecialClassLoader.findClass(ClassLoaders.scala:218) java.lang.ClassLoader.loadClass(ClassLoader.java:424) java.lang.ClassLoader.loadClass(ClassLoader.java:357) play.utils.Reflect$.loadClass$1(Reflect.scala:105) play.utils.Reflect$.configuredClass(Reflect.scala:124) play.api.ApplicationLoader$.apply(ApplicationLoader.scala:73) ammonite.$file.playtest$.(playtest.sc:65) ammonite.$file.playtest$.(playtest.sc)

Here's the code from playtest.sc

import scala.concurrent.Future

class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) {
  println("AppComponents...")

  val httpFilters    = Nil
  //private val Action = defaultActionBuilder

  val router: Router = Router.from {

    // Essentially copied verbatim from the SIRD example
    case GET(p"/hello/$to") =>
      defaultActionBuilder {
        Ok(s"Hello $to")
      }

    case GET(p"/sqrt/${double(num)}") =>
      defaultActionBuilder.async {
        Future {
          Ok(Math.sqrt(num).toString)
        }
      }

  }

}

class AppLoader extends ApplicationLoader {
  def load(context: Context) = new AppComponents(context).application
  println("AppLoader...")
}

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)

StdIn.readLine()

Play.stop(app)
sake92 commented 5 years ago

@gbersac There is no Play 2.5 for Scala 2.12 released, see here and here.
I suspect the same problem with ScalaJ version also.