I have a play framework application with a directory structure like this:
server
- app
- services
- MyService.scala
- project
- Dependencies.scala
- src/main/scala
- MyMetaService.scala
- build.sbt
The /project directory hosts a source code generator task (sbt Task) for which I'd like to be able to use Circe. My build.sbt file does include reference to the circe package and if I issue:
import io.circe._
in MyService.scala the package resolves fine. But if I do the same in MyMetaService.scala the package does not resolve. I know this is because somehow I'm not specifying that the dependency should apply to the /project directory but I don't know how to do that. Here's my build.sbt:
import src.main.scala.generate.ModelGenerator
name := "server"
version := "1.0"
lazy val `server` =
(project in file("."))
.settings(libraryDependencies ++= Dependencies.dependencies)
.enablePlugins(PlayScala)
resolvers ++=
Seq(
"Akka Snapshot Repository" at "http://repo.akka.io/snapshots/",
"scalaz-bintray" at "https://dl.bintray.com/scalaz/releases",
"releases" at "http://oss.sonatype.org/content/repositories/releases",
"snapshots" at "http://oss.sonatype.org/content/repositories/snapshots"
)
scalaVersion := "2.12.2"
sourceGenerators in Compile ++= Seq(
ModelGenerator.generatorTask.taskValue
)
And my Dependencies.scala:
import play.sbt.PlayImport._
import sbt._
object Dependencies {
val dependencies: Seq[ModuleID] =
Seq(
jdbc,
ehcache,
ws,
specs2 % Test,
guice,
"io.circe" %% "circe-core" % "0.11.1",
"io.circe" %% "circe-generic" % "0.11.1",
"io.circe" %% "circe-parser" % "0.11.1"
)
}
Create separate build.sbt for meta-build project in project/build.sbt and import dependencies in the same way as for proper build. For example
// This is project/build.sbt
val circeVersion = "0.10.0"
libraryDependencies ++= Seq(
"io.circe" %% "circe-core",
"io.circe" %% "circe-generic",
"io.circe" %% "circe-parser"
).map(_ % circeVersion)
should now make circe available to project/.../MyMetaService.scala
Related
I'm upgrading sbt-scalajs version from 0.6.x to 1.0.0.
This is my old plugins.sbt config
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.33")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "0.6.0")
My new plugins.sbt is
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")
Below is my old build.sbt
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
name := "sjs-test-error"
version := "0.1"
scalaVersion := "2.12.10"
val commonSettings = Seq(
scalaVersion := "2.12.10",
crossScalaVersions := Seq("2.12.10"),
scalacOptions ++= Seq("-feature", "-deprecation", "-Xlint", "-Ypartial-unification"),
Compile / compile / scalacOptions += "-Ywarn-unused-import",
Compile / doc / scalacOptions += "-no-link-warnings"
)
val core = crossProject(JSPlatform, JVMPlatform)
.in(file("core"))
.settings(commonSettings)
.settings(
name := "sjs-test-error",
)
.jsSettings(
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "0.9.1"
),
jsDependencies ++= Seq(
"org.webjars.npm" % "viz.js" % "1.7.0" / "1.7.0/viz.js"
)
)
lazy val coreJVM = core.jvm
lazy val coreJS = core.js
lazy val root = project.in(file("."))
.aggregate(coreJVM, coreJS)
.settings(commonSettings)
My new build.sbt is same except the import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType} is commented.
When I run sbt clean compile I get below error -
/Users/rajkumar.natarajan/Documents/Coding/OS/reftree/build.sbt:28: error: not found: value jsDependencies
jsDependencies ++= Seq(
^
/Users/rajkumar.natarajan/Documents/Coding/OS/reftree/build.sbt:31: error: value / is not a member of sbt.librarymanagement.ModuleID
"org.webjars.npm" % "viz.js" % "1.7.0" / "1.7.0/viz.js"
This is working fine in 0.6.x sbt-scalajs plugin. I don't know how to write the same for 1.0.0 version
Any idea how to fix this issue?
As mentioned in the release notes of Scala.js 1.0.0:
If you use jsDependencies (or rely on the jsDependencies of your transitive dependencies):
Add addSbtPlugin("org.scala-js" % "sbt-jsdependencies" % "1.0.0") in project/plugins.sbt
Add .enablePlugins(JSDependenciesPlugin) to Scala.js projects
Add .jsConfigure(_.enablePlugins(JSDependenciesPlugin)) to crossProjects
I want to use spring-data-neo4j with play framework 2.6 in Scala. But when I add the library dependency I have a compilation error:
object neo4j is not a member of package org
for
import org.neo4j.ogm.annotation.{GraphId, NodeEntity}
I think I don't have good resolvers:
name := """Project-name"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
resolvers += Resolver.sonatypeRepo("snapshots")
scalaVersion := "2.12.2"
libraryDependencies ++= Seq(
guice,
"org.scalatestplus.play" %% "scalatestplus-play" % "3.0.0" % Test,
"com.h2database" % "h2" % "1.4.194",
"org.springframework.data" % "spring-data-neo4j" % "4.2.6.RELEASE"
)
The classes you are trying to import import org.neo4j.ogm.annotation.{GraphId, NodeEntity} is part of neo4j-ogm-core artifact. so you must include it as shown below to able to access those classes.
libraryDependencies += "org.neo4j" % "neo4j-ogm-core" % "3.0.0-RC1"
I have a playframework subproject. Currently i'm defining it as a playframework module in the parent build.sbt
lazy val silhouetteModule = (project in file("modules/silhouette"))
.enablePlugins(PlayScala)
I would preffer to delegate that knowledge to the child project build.sbt . How can I call in the child project this enablePlugins?
import play.PlayScala
scalaVersion := "2.11.1"
name := "play-silhouette-seed"
version := "1.0"
libraryDependencies ++= Seq(
"com.mohiva" %% "play-silhouette" % "1.0",
"org.webjars" %% "webjars-play" % "2.3.0",
"org.webjars" % "bootstrap" % "3.1.1",
"org.webjars" % "jquery" % "1.11.0",
"net.codingwell" %% "scala-guice" % "4.0.0-beta4",
cache
)
//this doesn't work
currentThisProject.enablePlugins(PlayScala)
You can call enablePlugins directly in your build.sbt file for the subproject:
enablePlugins(PlayScala)
So I have a Scalatra app (using Scalatra 2.2.1). I'm building views using Scalate; I've decided to go with the Jade/Markdown one-two. Only one problem: if I try to use markdown in a jade template (started with the :markdown tag), I get this:
scala.Predef$.any2ArrowAssoc(Ljava/lang/Object;)Lscala/Predef$ArrowAssoc;
java.lang.NoSuchMethodError: scala.Predef$.any2ArrowAssoc(Ljava/lang/Object;)Lscala/Predef$ArrowAssoc;
at org.fusesource.scalamd.Markdown$.<init>(md.scala:119)
at org.fusesource.scalamd.Markdown$.<clinit>(md.scala:-1)
at org.fusesource.scalate.filter.ScalaMarkdownFilter$.filter(ScalaMarkdownFilter.scala:32)
at org.fusesource.scalate.RenderContext$class.filter(RenderContext.scala:276)
at org.fusesource.scalate.DefaultRenderContext.filter(DefaultRenderContext.scala:30)
at org.fusesource.scalate.RenderContext$class.value(RenderContext.scala:235)
at org.fusesource.scalate.DefaultRenderContext.value(DefaultRenderContext.scala:30)
at templates.views.$_scalate_$about_jade$.$_scalate_$render(about_jade.scala:37)
at templates.views.$_scalate_$about_jade.render(about_jade.scala:48)
at org.fusesource.scalate.DefaultRenderContext.capture(DefaultRenderContext.scala:92)
at org.fusesource.scalate.layout.DefaultLayoutStrategy.layout(DefaultLayoutStrategy.scala:45)
at org.fusesource.scalate.TemplateEngine$$anonfun$layout$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(TemplateEngine.scala:559)
at org.fusesource.scalate.TemplateEngine$$anonfun$layout$1$$anonfun$apply$mcV$sp$1.apply(TemplateEngine.scala:559)
at org.fusesource.scalate.TemplateEngine$$anonfun$layout$1$$anonfun$apply$mcV$sp$1.apply(TemplateEngine.scala:559)
So that's pretty cool. The error vanishes as soon as I remove the :markdown flag, and beyond that everything compiles (beyond markdown not getting rendered correctly).
Things I know and have found so far:
There's some thought that this error is the biproduct of incompatible Scala versions somewhere in the build. My build.scala defines Scala version as 2.10.0, which Scalatra is explicitly compatible with.
...that said, I have no idea which version of Scalate Scalatra pulls in, and my attempts to override it have not worked so far. I know the current stable of Scalate (1.6.1) is only compatible up to Scala 2.10.0 -- but that's what I'm using.
I am, however, sure that my classpath is clean. I have no conflicting Scala versions. Everything is 2.10.0 in the dependencies.
Has anybody worked with this one before? Any ideas?
EDIT
Per request, here are my build definitions:
//build.sbt
libraryDependencies += "org.scalatest" %% "scalatest" % "2.0.M5b" % "test"
libraryDependencies += "org.twitter4j" % "twitter4j-core" % "3.0.3"
libraryDependencies += "org.fusesource.scalamd" % "scalamd" % "1.5"
//build.properties
sbt.version=0.12.3
//build.scala
import sbt._
import Keys._
import org.scalatra.sbt._
import org.scalatra.sbt.PluginKeys._
import com.mojolly.scalate.ScalatePlugin._
import ScalateKeys._
object TheRangeBuild extends Build {
val Organization = "com.gastove"
val Name = "The Range"
val Version = "0.1.0-SNAPSHOT"
val ScalaVersion = "2.10.0"
val ScalatraVersion = "2.2.1"
lazy val project = Project (
"the-range",
file("."),
settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
organization := Organization,
name := Name,
version := Version,
scalaVersion := ScalaVersion,
resolvers += Classpaths.typesafeReleases,
libraryDependencies ++= Seq(
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-scalate" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "compile;container",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "compile;container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
),
scalateTemplateConfig in Compile <<= (sourceDirectory in Compile){ base =>
Seq(
TemplateConfig(
base / "webapp" / "WEB-INF" / "templates",
Seq.empty, /* default imports should be added here */
Seq(
Binding("context", "_root_.org.scalatra.scalate.ScalatraRenderContext", importMembers = true, isImplicit = true)
), /* add extra bindings here */
Some("templates")
)
)
}
) ++ seq(com.typesafe.startscript.StartScriptPlugin.startScriptForClassesSettings: _*)
)
}
When using the markdown filter, you need to add the scalamd library as runtime dependency:
"org.fusesource.scalamd" %% "scalamd" % "1.6"
The most recent version can be found on Maven Central
Also you can delete the build.sbt file and put the dependencies into build.scala file which makes things a bit simpler.
import sbt._
import Keys._
import org.scalatra.sbt._
import org.scalatra.sbt.PluginKeys._
import com.mojolly.scalate.ScalatePlugin._
import ScalateKeys._
object TheRangeBuild extends Build {
val Organization = "com.gastove"
val Name = "The Range"
val Version = "0.1.0-SNAPSHOT"
val ScalaVersion = "2.10.0"
val ScalatraVersion = "2.2.1"
lazy val project = Project(
"the-range",
file("."),
settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
organization := Organization,
name := Name,
version := Version,
scalaVersion := ScalaVersion,
resolvers += Classpaths.typesafeReleases,
libraryDependencies ++= Seq( // adding this Seq to the libraryDependencies
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-scalate" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "compile;container",
"org.scalatest" %% "scalatest" % "2.0.M5b" % "test",
"org.twitter4j" % "twitter4j-core" % "3.0.3",
"org.fusesource.scalamd" % "scalamd" % "1.6",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "compile;container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
),
scalateTemplateConfig in Compile <<= (sourceDirectory in Compile){ base =>
Seq(
TemplateConfig(
base / "webapp" / "WEB-INF" / "templates",
Seq.empty, /* default imports should be added here */
Seq(
Binding("context", "_root_.org.scalatra.scalate.ScalatraRenderContext", importMembers = true, isImplicit = true)
), /* add extra bindings here */
Some("templates")
)
)
}
) ++ seq(com.typesafe.startscript.StartScriptPlugin.startScriptForClassesSettings: _*)
)
}
This comes from:
libraryDependencies += "org.fusesource.scalamd" % "scalamd" % "1.5"
Looking at the scalamd-1.5 pom.xml, it is built against Scala 2.8.1, which is not compatible with 2.10.
Dependency resolution keeps 2.10 and discard the 2.8.1 dependency, and you end up with this classpath issue.
The only solution you have is to try and build a new scalamd version against Scala 2.10, potentially fix a few things to get it there, and then publish it (at least locally).
This sounds basic, but its actually cost me a whole day: I want to change to change the port that scalatra runs on, in development. I started with the hello world g8 template, and have been building from there.
Here's what I've tried so far:
Changing the port in build.scala, ala documentation:
http://www.scalatra.org/guides/deployment/configuration.html
This doesn't compile, because port is undefined.
Changing the port in build.scala, ala these two examples:
https: gist.github.com dozed 58af6cfbfe721a562a48
https://github.com/JamesEarlDouglas/xsbt-web-plugin/blob/master/src/sbt-test/web/servlet/project/Build.scala
Same problem: port is undefined
Redefining the entry point, ala
http: www.scalatra.org guides deployment standalone.html
Still runs on port 8080
Changing init params in bootstrap, ala
http: www.scalatra.org guides deployment configuration.html
Still runs on port 8080
Any help appreciated. I can't post more than 2 links for some reason, so replace spaces with forward slashes to follow the urls.
Here's my build.scala in case it helps.
import sbt._
import Keys._
import org.scalatra.sbt._
import org.scalatra.sbt.PluginKeys._
import com.mojolly.scalate.ScalatePlugin._
import ScalateKeys._
import com.earldouglas.xsbtwebplugin._
import WebPlugin._
object YesManBuild extends Build {
val Organization = "com.prezi"
val Name = "Yes Man"
val Version = "0.1.0-SNAPSHOT"
val ScalaVersion = "2.10.2"
val ScalatraVersion = "2.2.1"
//def Conf = config("container")
lazy val project = Project (
"yes-man",
file("."),
settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
//port in Conf := 8081,
mainClass := Some("com.prezi.eureka.JettyLauncher.main"),
organization := Organization,
name := Name,
version := Version,
scalaVersion := ScalaVersion,
resolvers += Classpaths.typesafeReleases,
libraryDependencies ++= Seq(
"org.slf4j" % "slf4j-log4j12" % "1.7.5",
"com.netflix.eureka" % "eureka-client" % "1.1.97",
"com.netflix.ribbon" % "ribbon-httpclient" % "0.1.10",
"com.netflix.ribbon" % "ribbon-eureka" % "0.1.11",
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-scalate" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "container",
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")),
"org.eclipse.jetty.aggregate" % "jetty-all" % "9.0.4.v20130625"
),
scalateTemplateConfig in Compile <<= (sourceDirectory in Compile){ base =>
Seq(
TemplateConfig(
base / "webapp" / "WEB-INF" / "templates",
Seq.empty, /* default imports should be added here */
Seq(
Binding("context", "_root_.org.scalatra.scalate.ScalatraRenderContext", importMembers = true, isImplicit = true)
), /* add extra bindings here */
Some("templates")
)
)
}
)
)
}
Thanks guys,
~Erik
Update 23.01.2016: scalatra-sbt uses xsbt-web-plugin 2.0.4 and a few settings changed. You can find the xsbt-web-plugin docs here (related note: integrations for xsbt-web-plugin, sbt-web, docker and standalone builds can be found in https://github.com/scalatra/scalatra-in-action, see chapter09-* directories).
For a Scalatra app this means:
use jetty:start, jetty:stop instead container:start, container:stop
enable JettyPlugin
use new keys, e.g. containerPort in Jetty := 8090, target in webappPrepare, sourceDirectory in webappPrepare
only .scala based SBT build definition: use the correct imports to import the plugin and setting keys
A .sbt build definition:
organization := "org.foo"
name := "My build"
version := "0.1.0-SNAPSHOT"
scalaVersion := "2.11.6"
val ScalatraVersion = "2.4.0"
libraryDependencies ++= Seq(
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"com.typesafe" % "config" % "1.2.1",
"ch.qos.logback" % "logback-classic" % "1.1.3" % "runtime",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
)
enablePlugins(JettyPlugin)
containerPort in Jetty := 8090
A .scala based SBT build definition does have bit less magic, and we need to import the plugin and its settings:
import sbt._
import Keys._
import org.scalatra.sbt._
import com.earldouglas.xwp.JettyPlugin
import com.earldouglas.xwp.JettyPlugin.autoImport._
import com.earldouglas.xwp.ContainerPlugin.autoImport._
The actual build definition with enablePlugins(JettyPlugin) and a custom port:
object MyBuild extends Build {
val Organization = "org.foo"
val Name = "My Build"
val Version = "0.1.0-SNAPSHOT"
val ScalaVersion = "2.11.6"
val ScalatraVersion = "2.4.0"
val mySettings =
ScalatraPlugin.scalatraSettings ++ Seq(
organization := Organization,
name := Name,
version := Version,
scalaVersion := ScalaVersion,
libraryDependencies ++= Seq(
"org.scalatra" %% "scalatra" % ScalatraVersion,
"org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
"com.typesafe" % "config" % "1.2.1",
"ch.qos.logback" % "logback-classic" % "1.1.3" % "runtime",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
),
containerPort in Jetty := 8090
)
lazy val project = Project("chapter09", file("."))
.enablePlugins(JettyPlugin)
.settings(mySettings: _*)
}
Make sure you are using the imports:
import com.earldouglas.xsbtwebplugin.PluginKeys._
import com.earldouglas.xsbtwebplugin.WebPlugin._
With those imports you can use then the correct key and configuration:
port in container.Configuration := 9000
This goes in the settings block:
...
lazy val project = Project (
"example",
file("."),
settings = Defaults.defaultSettings ++ ScalatraPlugin.scalatraWithJRebel ++ scalateSettings ++ Seq(
port in container.Configuration := 9000,
organization := Organization,
...
scalatra-sbt builds on xsbt-web-plugin whose settings are documented here: https://github.com/JamesEarlDouglas/xsbt-web-plugin/wiki/Settings
I did this and worked:
....
val port = SettingKey[Int]("port")
val Conf = config("container")
lazy val project = Project (
port in Conf := 8081,
....
)
I created an empty build.sbt file in the root of my project and put:
port in container.Configuration := 8090
in the file for it to work.