object db is not a member of package play - scala

I'm trying to make my first tests with Scala and with Play framework.
I have installed play 2.2.0, which seems to be the last version, with the standalone package. After that, I've been able to create a new application, compile and run it.
I've tried to start to use Anorm package to access to the database, but I've found a blocking error which I can't find on the docs. I don't know if that means that is so obvious, but after adding:
package controllers
import play.api._
import play.api.mvc._
import play.db.anorm._ //(this is the new line)
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
It fails with:
object db is not a member of package play
I've seen this:
https://groups.google.com/forum/#!msg/play-framework/RUbmEVsw2rY/UOE5mNs1WjoJ
Where they talk about adding the dependency to jdbc, which seems to be already in my build.sbt.
libraryDependencies ++= Seq(
jdbc,
anorm,
cache
)
I've also found this thread here:
play.db package not found in play 2.1.3
But I can't find a build.scala file on my project. Not using any IDE by now, just play console (run & compile commands).
Thanks a lot!

In fact (as the error explains), there is no package play.db.anorm._ in version 2.2.0. Try use import anorm._ instead.

You need the following libraries
slick
play-jdbc
anorm
This is how my dependencies look like in build.sbt :
libraryDependencies ++= Seq(
"com.typesafe.slick" % "slick_2.10" % "2.1.0",
"org.postgresql" % "postgresql" % "9.4-1201-jdbc41",
"com.typesafe.play" % "play-jdbc_2.10" % "2.4.0-RC1",
cache,
anorm
)
Search for the latest version of library at Maven Central Repository

Related

How to add dedendency in SBT project

I am new scala and SBT. I am trying write a pursuing engine which can read the JSON string to object and write back Object to JSON string. To do that I am using eclipse as IDE and SBT(0.13.13) as build tool and jackson. I am getting some error while trying to import the dependency in scala file. My steps as follows:
I have added this two dependency in build.sbt of my project
libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.5.3"
libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.8.8"
Compile the project from SBT console with "compile" command
Creating a scala object for DAO object, which will contain the data and transfer as JSON
But when I am trying to import this in scala source file:
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
It's giving some compilation error:
object fasterxml is not a member of package com
But my compilation, which I did to download those dependency, ends up with success and showing download as [SUCCESSFUL]
I think I am missing something, can any one help me out in this regard?
As part of experiment I have added MySql dependency in my project as:
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.40"
Which is working properly, I am able to connect the MySql Server from scala code.
One more thing as in Maven we can update or if we save the POM file, maven downloads all those dependency automatically. Is that the same in SBT or every time after adding dependency in build.sbt I have to "reload" and "compile" to download?
Here are some of the steps you need to follow
1) clean your project in eclipse (eclipse might cache dependencies)
2) make sure your project directories are as explained in here
3) If above two steps doesn't work do
import _root_.com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import _root_.com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import _root_.com.fasterxml.jackson.module.scala.DefaultScalaModule
Hope you get it solved

How can I externalize these play libraries

I started to use a project/Settings.scala to help clean up my main build.sbt. This is a scalajs project with Play backend where I use Play's WS & Cache dependencies. In built.sbt the 'string/keyword/' <-not sure correct term here but ws and cache properly resolve. However in my Seq[String] in Settings.scala where I store my server library dependencies they won't. Currently I am using
libraryDependencies ++= Seq(ws, cache) ++ Settings.jvmDependencies.value,
which works but it makes me wonder how I might be able to move everything to Settings or if that is possible. When I dig one layer deeper in IDE I see for example ws is defined as :
val ws : sbt.ModuleID = { /* compiled code */ }
in the object PlayImport but I can't see the proper values to populate a full/typical dependency definition for sbt
Ultimately I am curious can I successfully export ws & cache such that I can have this line in my build.sbt
libraryDependencies ++= Settings.jvmDependencies.value,
You can have a direct look at the Play SBT plugin sources, it's usually the easiest way. Here's how the ws defined:
val ws = component("play-ahc-ws")
where component is defined in the same file like this:
def component(id: String) = "com.typesafe.play" %% id % play.core.PlayVersion.current
With this information we know the ws dependency amounts to "com.typesafe.play" %% "play-ahc-ws" % "2.5.10" for the current Play version.
If you want to have all the Play symbols in your plugin - which is what your project/Settings.scala file is - just import the fields from the Play plugin's autoImport member:
import play.sbt.Play.autoImport._
This will let you use ws, cache, and any other symbols the plugin exposes.

Scala Playframework 2.2 - HtmlUnit missing

I have a project using Playframework 2.2
I eclipsified it and in my code I write something like this:
import com.gargoylesoftware.htmlunit.WebClient
val client = new WebClient
Eclipse says everything's fine, but when I play run my application I get a following message:
[error] C:\workspace\kwiket\kwiketscala\app\service\parsers\HTMLParser.scala:11: object gargoylesoftware is not a member of package com
[error] import com.gargoylesoftware.htmlunit.WebClient
How could I fix this? What dependencies should I add to my project? Thanks
In the test configuration is HTMLUnit available, but you need it in the compile configuration.
Just update your libraryDependencies in build.sbt:
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
"net.sourceforge.htmlunit" % "htmlunit" % "2.13" //found on http://search.maven.org/#artifactdetails%7Cnet.sourceforge.htmlunit%7Chtmlunit%7C2.13%7Cjar section SBT
)
Restart SBT or execute reload.

Play framework 2.2 Slick dependency issue

I'm trying to follow this tutorial, but I get stuck as soon as trying to import necessary packages:
import scala.slick.session.Database
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
I'm getting these errors:
object slick is not a member of package scala
not found: object Database
object slick is not a member of package scala
My SBT dependencies:
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
"com.typesafe.slick" %% "slick" % "1.0.1",
"com.typesafe.play" %% "play-slick" % "0.5.0.8"
)
What am I missing here?
I'm running Play Framework 2.2.0, Scala 2.10.3
You probably need to run some of these commands in sbt (in this order):
reload
update
eclipse
(obviously substitute eclipse with whatever IDE you are using, or remove it if you are not using an IDE.)

How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?

So I've been trying to get IDEA 12.0 to work with Play 2.1.0 and Scala 2.10.0. I've just about given up because it's not working for me the way I want it to. Here is a copy of my build.properties, Build.scala, and plugins.sbt. I followed the approach on the playframework site to execute idea with-sources=yes in the play console. I've also tried adding sbt-idea plugin version 1.3.0-SNAPSHOT as seen in plugins.sbt, but nothing seems to work if I want to reference a new view template I just created or a new route. The only way I can work in IDEA is if I have a console open and run sbt compile, go back to IDEA, and it will refresh itself and recognize the new view templates or routes.
plugins.sbt
logLevel := Level.Warn
scalaVersion := "2.10.0"
// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Sonatype snapshots to get sbt-idea 1.3.0-SNAPSHOT
//resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.1.0")
//addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.3.0-SNAPSHOT")
build.properties
sbt.version=0.12.2
Build.scala
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "admin-application"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
jdbc,
anorm
)
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
)
}
If you use IDEA Community edition, there is a SBT Console plugin (see http://plugins.jetbrains.com/plugin?pluginId=5007) that allows you to compile / run your Play project directly in the editor. That's the way I work every day and it is fine (I use the ~run command and then don't care anymore).
You may also add a remote debugger in IDEA that listens to you local server (it it is run with debug mode on) and use it as usual.
If you use IDEA Ultimate edition, JetBrains released a Play Framework plugin that seems to work fine (but I haven't tested it yet). Have a look at these tutorials:
http://confluence.jetbrains.com/display/IntelliJIDEA/Play+Framework+2.0 or
http://www.jamesward.com/2013/01/23/video-create-and-run-play-framework-apps-in-intellij
Hope this helps.
I think this is how it work currently. As suggested by #pedrofurla, you can keep ~run running on sbt/play console. Sadly, IMO there is no other way IntelliJ can compile your scala views automatically.
Just add to project/plugins.sbt the following and re-run play idea
// FIX SBT IDEA PLAY 2.1
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.1.0")