Explain Build.scala in play framework - scala

Can somebody explain the syntax for Build.scala in detail? For example, I have the following Build.scala:
import sbt._
import Keys._
import play.Project._
object Build extends sbt.Build {
val appName = "myapp"
val appVersion = "1.0"
val appDependencies = Seq(
"postgresql" % "postgresql" % "9.1-901-1.jdbc4",
javaCore,
javaJdbc,
javaEbean,
"org.json" %"org.json" % "chargebee-1.0",
"org.reflections" % "reflections" % "0.9.8",
"org.mockito" % "mockito-all" % "1.9.5" % "test"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
libraryDependencies += "com.jolbox" % "bonecp" % "0.8.0-rc2-SNAPSHOT",
resolvers += Resolver.url("sbt-plugin-snapshots", new URL("http://repo.scala-sbt.org/scalasbt/sbt-plugin-snapshots/"))(Resolver.ivyStylePatterns),
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
)
}
How do I match the appDependencies with the resolvers? Or how do I know what resolvers I need to add for what appDependencies? Where do I look in the resolver's repo? What is libraryDependencies? How are they different from appDependencies?
Thanks.

There are a few things we need to disentangle here.
Let's first distinguish between an sbt build file and Build.scala. While the build file is exactly like a Maven pom or a Gradle build file, think of Build.scala as a build capability with the full power and expressiveness of Scala because it is a Scala class like any other. I believe the gap between the two has narrowed though with the latest version of sbt.
Now in both sbt and Build.scala, you have the notion of library dependencies, which are the jar libraries containing code you can use for your projects. These libraries can be found in lots of places--Maven repositories, the local file system, etc. You use resolvers to specify those locations.
So you tell both which jars you need and where to find them.
As for appDependencies, that's not actually a thing. As I said, Build.scala is a class like any other, and appDependencies is just a variable name. It just makes sense to use that name because that Seq is what you will pass to the Project constructor.

Related

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.

Having SBT 0.11 pulling in querulous from github by using information in build.sbt

I would like to just add some information to build.sbt and have it pull in
https://github.com/twitter/querulous
How can this be done?
Add the following lines:
libraryDependencies += "com.twitter" %% "querulous-core" % "2.7.0"
resolvers += "Twitter repo" at "http://maven.twttr.com"
Note also, that querulous is kinda abandoned, so you may want to switch to something else.
EDIT
If you looking for ways to fetch querulous directly from github, AFAIK there is no one via build.sbt but you can configure you can do this via project/MyBuild.scala:
import sbt._
import Keys._
object MyBuild extends Build {
lazy val root = Project(".", file("Foo"),
settings = Defaults.defaultSettings
) dependsOn(querulous)
lazy val querulous = uri("git://github.com/twitter/querulous")
}

Scala & XSBT - share part of build classpath with project classpath

First of all I'm very new to Scala, SBT and Vaadin.
I'm trying to make an Scala & Vaadin application with Timeline addon. I had to put Timeline jar (unmanaged dependency) and Vaadin dependency under build project (project/) so I can compile it during build (well, by calling vaadin task). How can I merge vaadin-timeline-agpl-3.0-1.2.3.jar and Vaadin dependency into vaadinTest project without duplicating jar and dependency? Below is my project structure. Thank you. By the way, I already tried existing Vaadin plugins but they suck (all I need to do is to compile widgetset).
This is my project structure:
vaadinTest/
project/
lib/vaadin-timeline-agpl-3.0-1.2.3.jar
build.sbt
ProjectBuild.scala
src/
vaadinTest/project/build.sbt:
libraryDependencies ++= Seq(
"com.google.gwt" % "gwt-user" % "2.4.0",
"com.google.gwt" % "gwt-dev" % "2.4.0",
"com.vaadin" % "vaadin" % "6.7.4"
)
vaadinTest/project/ProjectBuild.scala:
import com.google.gwt.dev.Compiler
import sbt._
import Keys._
object ProjectBuild extends Build {
val deps = Seq(
"org.eclipse.jetty" % "jetty-server" % "8.1.0.RC5",
"org.eclipse.jetty" % "jetty-webapp" % "8.1.0.RC5",
"org.eclipse.jetty" % "jetty-servlet" % "8.1.0.RC5",
)
val vaadinTest = Project(
"vaadinTest", file("."), Defaults.defaultSettings
++ Seq(libraryDependencies ++= deps)
++ Seq(vaadinTask)
)
val vaadin = TaskKey[Unit]("vaadin")
val vaadinTask = vaadin := {
Compiler.main(Array("-war", "src/main/webapp/VAADIN/widgetsets",
"com.vaadin.addon.timeline.gwt.TimelineWidgetSet"))
}
}
Your question mentions a "plugin" project, but I can't see that project in your project structure or ProjectBuild.scala?
In any case, you can add additional directories to your classpath by modifying the dependencyClasspath setting. This is documented here (see the "Unmanaged dependencies" section).

LIFT setup for web project: generate with mvn, and manage with sbt

I am new to LIFT, and I am trying to find a reliable instructions how to generate and manage LIFT web project with maven and sbt respectively. Can someone please direct me (or provide here) to the up to date instructions how to setup sbt for the maven generated project? From every post what I've red, it looks like the best setup for the LIFT projects: generate with mvn, manage with sbt. Will you agree? (I cannot generate LIFT/web project with sbt. Right? SBT is only good for managing it. Right? ) Every instructions I tried are out of date though. (I obviously can simply download and un-tar the archetype project, but I want to find a more fundamental approach for managing the environment ) Thanks.
While I'm using lift I don't need a maven at all, just SBT. So, it is very useful to read SBT Getting Started section. Also lift wiki contains some information. But be sure that you read material related to the proper SBT version. And finally, you can pay attention to my lift project template on github.
Good Luck with Lift! It's awesome ;)
By following question in you comment I put here some common config from my projects.
So, that is ./project/build.scala as alternative to ./build.sbt
import sbt._
import Keys._
import com.github.siasia._
import PluginKeys._
import WebPlugin._
import WebappPlugin._
object LiftProjectBuild extends Build {
override lazy val settings = super.settings ++ buildSettings
lazy val buildSettings = Seq(
organization := "com.yourorganization",
version := "0.1-SNAPSHOT",
scalaVersion := "2.9.1")
def yourWebSettings = webSettings ++ Seq(
// If you are using jrebel
scanDirectories in Compile := Nil
)
lazy val shade = Project(
id = "project-name",
base = file("."),
settings = defaultSettings ++ yourWebSettings)
lazy val defaultSettings = Defaults.defaultSettings ++ Seq(
name := "project-name",
resolvers ++= Seq(
"Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases",
"Java.net Maven2 Repository" at "http://download.java.net/maven/2/"),
libraryDependencies ++= {
val liftVersion = "2.4-M5"
Seq(
"net.liftweb" %% "lift-webkit" % liftVersion % "compile",
"org.eclipse.jetty" % "jetty-webapp" % "7.5.4.v20111024" % "container",
"org.squeryl" %% "squeryl" % "0.9.5-SNAPSHOT" % "compile",
"ch.qos.logback" % "logback-classic" % "1.0.0" % "compile",
"org.scalatest" %% "scalatest" % "1.6.1" % "test",
"junit" % "junit" % "4.10" % "test")
},
// compile options
scalacOptions ++= Seq("-encoding", "UTF-8", "-deprecation", "-unchecked"),
javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"),
// show full stack traces
testOptions in Test += Tests.Argument("-oF")
)
}
./project/build.properties
#Project properties
sbt.version=0.11.1
./project/plugins.sbt
resolvers += Classpaths.typesafeResolver
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse" % "1.5.0")
libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % (v+"-0.2.10"))
Having these three files are enough to configure sbt.
And of course you can run your application by calling container:start
Your mileage may vary, but my experience with sbt has been less than stellar (out-of-date docs, breakage on version changes, etc). The recent improvements to the eclipse scala IDE and corresponding maven and jrebel plugins make it a clear winner compared to using sbt.
If you follow the instructions, you'll get the ability to build/package at the command line, but superior support for eclipse features and fast development.
See the sample project setup at:
https://github.com/awkay/lift_squeryl_demo