Play framework's sbt import maven module error - scala

I am using Playframework 2.5.4 to serve as REST API endpoint in default sbt build, compile & run with activator.
This play module imports a core module in local maven (3.9), where types were defined. Such as:
package com.myCompany
case class User(
id: Option[UUID] = None,
username: String
)
Then play controller package will import com.myCompany.User.
Issue is, when activator compiles, it gives unknown parameter name: username
If I placed case class in play's models package, it compiles without error.
Below is my build.sbt
name := """api"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
filters,
"com.myCompany" % "core" % "1.0-SNAPSHOT"
)
routesGenerator := InjectedRoutesGenerator
resolvers ++= Seq(
"scalaz-bintray" at "http://dl.bintray.com/scalaz/releases",
Resolver.mavenLocal
// "Local Maven Repository" at "file:///home/pocheng/.m2/repository"
)
I suspect build.sbt file is having issue. Would appreciate any pointer. Thanks.

Related

How to add Google ML Kit dependency to SBT project?

When adding the Google ML Kit dependency to our SBT project, it successfully compiles, yet it cannot resolve imports to the com.google.mlkit package.
Here is a sample build.sbt file, using barcode-scanning as an example:
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.13.8"
lazy val root = (project in file("."))
.settings(
name := "test-google-mlkit"
)
resolvers += "Google Maven" at "https://maven.google.com/"
libraryDependencies += "com.google.mlkit" % "barcode-scanning" % "17.0.2" // % "runtime"
This successfully compiles, but we cannot resolve an import to com.google.mlkit, with or without the "runtime" flag (as indicated on the maven repo).
object mlkit is not a member of package com.google
Is it possible to use Google's ML Kit in a Scala project? What are we missing? Any help is appreciated.
To solve the import problem just change the resolvers to:
resolvers += "google repository" at "https://dl.google.com/android/maven2"

Play framework compilation issues with dependencies

I'm having issues with running a simple scala play app with a few dependencies. The below is happening when trying to run my app.
[error] /Users/roland/play-scala/app/domain/UserModule.scala:2:
object softwaremill is not a member of package com
[error] import com.softwaremill.macwire.MacwireMacros.wire
UserModule:
package domain
import com.softwaremill.macwire.MacwireMacros.wire
trait UserModule {
lazy val userRepository = wire[UserRepository]
lazy val userService = wire[UserService]
}
and my build.sbt is
name := """play-scala"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.10.3"
resolvers += "Maven Central Server" at "http://repo1.maven.org/maven2"
resolvers += "Neo4j Scala Repo" at "http://m2.neo4j.org/content/repositories/releases"
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
ws,
"org.webjars" % "coffee-script-node" % "1.7.1",
"net.liftweb" %% "lift-json" % "3.0-SNAPSHOT",
"eu.fakod" %% "neo4j-scala" % "0.3.1-SNAPSHOT",
"com.softwaremill.macwire" %% "macros" % "0.7.3",
"com.softwaremill.macwire" %% "runtime" % "0.7.3"
)
It seems to be totally fine when looking at it in intellij idea and looking in my ivy cache I have the dependencies there but the application when it runs through the play console seems to be very unhappy. Sorry for the lack of info but im quite new to scala and play so been struggling all day with this issue.
Indeed the maven repo is named macros_2.10 but there is scalaVersion := "2.10.3" in build.sbt
Try to remove this line.

Unable to add scala-reflect as a dependency

I can't add scala-reflect as dependency. My project/build.sbt looks like this:
//the name of the project, will become the name of the war file when you run the package command.
name := "Test-SBT"
version := "1.0"
//specify which Scala version we are using in this project.
scalaVersion := "2.10.3"
libraryDependencies <++= (scalaVersion)(sv =>
Seq(
"org.scala-lang" % "scala-reflect" % "2.10.3",
"org.scala-lang" % "scala-compiler" % "2.10.3"
)
)
And project/build.properties
sbt.version=0.13.0
And here is my Main class:
object Main1 extends App {
import scala.reflect.runtime.universe
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
//......
}
It says object runtime is not a member of package reflect. Of course, I did "gen-idea", "clean" and other things. What's up with that?
Guessing here due the question by #laughedelic.
The build.sbt should be in the root. Assuming the project you are writing is in test-sbt, you should end up with structure similar to:
test-sbt/build.sbt
test-sbt/project
Otherwise the build.sbt is used in creating the "internal compile project" used by SBT.
A deeper explanation can be found at SBT's docs sbt is recursive.

How to run playframework sample in Intelllij 13?

I am using Intellij 13 Ultimate and want to create a Play Framework sample. But I am unable to build this project because it always throws this error:
object index is not a member of package views.html
Ok(views.html.index("Your new application is ready."))
I have tried this on both Mac and Windows platforms, but the error is always the same.
How can I solve this?
The generation works just fine and all the paths are correctly added to the build. However the routes are not (yet) compiled by the plugins (scala+playframework), thus the reverse routing classes generated by play are not available for intellij.
You will have the same problem with the templates.
If you run a play compile (or a sbt compile), the classes will be generated and intellij should be able to pick them up and compile your project.
I found a trick here.
If you generate the play-scala project using activator command line activator [new my_first_project play-scala]. You'll get the following sbt build file.
name := """my_first_project"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
But IF you create a project from intellj using NewProject->Scala-Play 2.x, you will get the following sbt.
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
after combine them. Ignore the name. And I set for in run to false
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
After doing that. Open the activator-generated project with Intellj, configure a run with Play 2.x run. Everything goes well.
By the way, if you open the activator-generated project before you change the sbt file. You might need to rm -r .idea
Hope that helps.
Running "play idea" in the new project root fixed it for me. The project should compile and run after reloading.
I only have this problem with projects created through Idea's New Project menu.
I have the same problem: in Idea 13 there are highlight errors there, but in Idea 12 everything is ok. The reason is that there is no Scala plugin for Idea 13, so for now it's impossible to get it to work in Idea 13.
The solution is to install Idea 12, go to Preferences -> Plugins, type "Scala" in find box, and install Scala plugin.

How to add "provided" dependencies back to run/test tasks' classpath?

Here's an example build.sbt:
import AssemblyKeys._
assemblySettings
buildInfoSettings
net.virtualvoid.sbt.graph.Plugin.graphSettings
name := "scala-app-template"
version := "0.1"
scalaVersion := "2.9.3"
val FunnyRuntime = config("funnyruntime") extend(Compile)
libraryDependencies += "org.spark-project" %% "spark-core" % "0.7.3" % "provided"
sourceGenerators in Compile <+= buildInfo
buildInfoPackage := "com.psnively"
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, target)
assembleArtifact in packageScala := false
val root = project.in(file(".")).
configs(FunnyRuntime).
settings(inConfig(FunnyRuntime)(Classpaths.configSettings ++ baseAssemblySettings ++ Seq(
libraryDependencies += "org.spark-project" %% "spark-core" % "0.7.3" % "funnyruntime"
)): _*)
The goal is to have spark-core "provided" so it and its dependencies are not included in the assembly artifact, but to reinclude them on the runtime classpath for the run- and test-related tasks.
It seems that using a custom scope will ultimately be helpful, but I'm stymied on how to actually cause the default/global run/test tasks to use the custom libraryDependencies and hopefully override the default. I've tried things including:
(run in Global) := (run in FunnyRuntime)
and the like to no avail.
To summarize: this feels essentially a generalization of the web case, where the servlet-api is in "provided" scope, and run/test tasks generally fork a servlet container that really does provide the servlet-api to the running code. The only difference here is that I'm not forking off a separate JVM/environment; I just want to manually augment those tasks' classpaths, effectively "undoing" the "provided" scope, but in a way that continues to exclude the dependency from the assembly artifact.
For a similar case I used in assembly.sbt:
run in Compile <<= Defaults.runTask(fullClasspath in Compile, mainClass in (Compile, run), runner in (Compile, run))
and now the 'run' task uses all the libraries, including the ones marked with "provided". No further change was necessary.
Update:
#rob solution seems to be the only one working on latest SBT version, just add to settings in build.sbt:
run in Compile := Defaults.runTask(fullClasspath in Compile, mainClass in (Compile, run), runner in (Compile, run)).evaluated,
runMain in Compile := Defaults.runMainTask(fullClasspath in Compile, runner in(Compile, run)).evaluated
Adding to #douglaz' answer,
runMain in Compile <<= Defaults.runMainTask(fullClasspath in Compile, runner in (Compile, run))
is the corresponding fix for the runMain task.
Another option is to create separate sbt projects for assembly vs run/test. This allows you to run sbt assemblyProj/assembly to build a fat jar for deploying with spark-submit, as well as sbt runTestProj/run for running directly via sbt with Spark embedded. As added benefits, runTestProj will work without modification in IntelliJ, and a separate main class can be defined for each project in order to e.g. specify the spark master in code when running with sbt.
val sparkDep = "org.apache.spark" %% "spark-core" % sparkVersion
val commonSettings = Seq(
name := "Project",
libraryDependencies ++= Seq(...) // Common deps
)
// Project for running via spark-submit
lazy val assemblyProj = (project in file("proj-dir"))
.settings(
commonSettings,
assembly / mainClass := Some("com.example.Main"),
libraryDependencies += sparkDep % "provided"
)
// Project for running via sbt with embedded spark
lazy val runTestProj = (project in file("proj-dir"))
.settings(
// Projects' target dirs can't overlap
target := target.value.toPath.resolveSibling("target-runtest").toFile,
commonSettings,
// If separate main file needed, e.g. for specifying spark master in code
Compile / run / mainClass := Some("com.example.RunMain"),
libraryDependencies += sparkDep
)
If you use sbt-revolver plugin, here is a solution for its "reStart" task:
fullClasspath in Revolver.reStart <<= fullClasspath in Compile
UPD: for sbt-1.0 you may use the new assignment form:
fullClasspath in Revolver.reStart := (fullClasspath in Compile).value