How to install ReactiveMongo on play 2.4?

I have installed the following: 1.Play 2.4 2.Created a scala project 3.added the eclipse plugin

Now I wanted to add a database connection. I want to tryout ReactiveMongo, but the instructions on the wiki page are for 2.3 or older.

https://github.com/ReactiveMongo/Play-ReactiveMongo

For 2.4 it seems like the file structure of play has changed. I need to know the proper way to configure play 2.4 for ReactiveMongo.

Here are the instructions that they give for play versions newer than 2.4:

If you want to use the latest snapshot, add the following instead (only for play > 2.3):

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

libraryDependencies ++= Seq(
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.0-SNAPSHOT"
)

Configure your application to use ReactiveMongo plugin
add to your conf/play.plugins

1100:play.modules.reactivemongo.ReactiveMongoPlugin

Configure your database access within application.conf

How would I apply there configuration to the new file structure of play 2.4?

This is what I attempted to do with no success: In the project/plugins.sbt I added:

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"


addSbtPlugin("org.reactivemongo" % "play2-reactivemongo" % "0.11.0-SNAPSHOT")

I get a resolving error message:

        at java.lang.Thread.run(Thread.java:745)
[error] (*:update) sbt.ResolveException: unresolved dependency: org.reactivemong
o#play2-reactivemongo;0.11.0-SNAPSHOT: not found

So, after learning that I needed to add the dependency to the /build.sbt file and made the changes there.

name := """oneid-scala"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  specs2 % Test
)

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

//This is for reactivemongodb
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

//This is for reactivemongodb
libraryDependencies ++= Seq(
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.0-SNAPSHOT"
)

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

EclipseKeys.createSrc := EclipseCreateSrc.All

After doing these steps I wanted to verify if I did the installation correctly. So I attempted to add the tutorial code to my project from https://github.com/ReactiveMongo/Play-ReactiveMongo

/app
     /controllers/Application.scala
     /controllers/UsingJsonReadersWriters.scala
     /models/models.scala
/conf
    /routes

Then I do an activator clean Then I do a activator run

I see an error after the run:

missing or invalid dependency detected while loading class file 'JSONGenericHandlers.class'.
Could not access type GenericHandlers in package reactivemongo.api.collections, because it (or its dependencies) are missing.
Check your build definition for missing or conflicting dependencies.
(Re-run with `-Ylog-classpath` to see the problematic classpath.) 
A full rebuild may help if 'JSONGenericHandlers.class' was compiled against an incompatible version of reactivemongo.api.collections.

So, it seems that my install failed. So, this question is still open.


Add the following line to your build.sbt :

"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24"

eg:

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  specs2 % Test,
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24",
  "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
)

As for the examples on the reactive mongo,I never got them working. I think they may be a little out of date. Try (Not the best example but simple):

import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.BSONDocument
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

class RMongoTest(){
  def run = {
    val dbHosts: List[String] = List("localhost")
    val dbName = "TestDB"
    val driver = new MongoDriver
    val connection = driver.connection(dbHosts)
    val db = connection("TestDB")
    val collection :BSONCollection = db("TestCollection")

    val futureInsert = collection.insert(BSONDocument("Moo" -> "Over"))
    Await.result(futureInsert, 10.seconds)  //Not really a great pattern...

    val futureResult = collection.find(BSONDocument()).one
    val result = Await.result(futureResult, 10.seconds)
    println(result.get.get("Moo"))
  }
}

If anybody is still looking for answers: In Play 2.4.x the dependency to be added to your build.sbt looks like this...

For ReactiveMongo 0.11.7 :

libraryDependencies ++= Seq(
  // ...
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play24"
)

For ReactiveMongo 0.11.9 (released on 20 December 2015):

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  // ...
  "org.reactivemongo" % "play2-reactivemongo_2.11" % "0.11.9"
  // Or to let SBT add the Scala version suffix automatically:
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.9"
)

Note that the naming seems to change from time to time. For later releases you can try these steps:

  • Go to the Maven Central Repository and search for g:"org.reactivemongo" play .
  • Look for an ArtifactId that ends roughly with your Scala version, eg play2-reactivemongo_2.11 if you have Scala 2.11.x .
  • Click on the version number (eg 0.11.9 ) in that line, which takes you to a page like this.
  • In the section Dependency Information, click on "Scala SBT".
  • Copy the line into your build.sbt file. If you have a libraryDependencies ++= Seq(...) section, copy only the part after += .
  • 链接地址: http://www.djcxy.com/p/95142.html

    上一篇: 如何找出哪些依赖包含错误版本的Play / Scala在sbt中

    下一篇: 如何在Play 2.4上安装ReactiveMongo?