Manage dependencies with sbt & IntelliJ IDEA - scala

I'm working on a back-end project using for the first time Scala and the Play Framework with IntelliJ IDEA.
I've been reading a lot of documentation and topics:
SBT Library dependencies doc
IntelliJ support post
Another post from stackoverflow
I can't understand how to import dependencies.
When I try to append a new dependency, IntelliJ underlines it and shows either "Unknown artifact. Not resolved or indexed" or a log shows up and says "SBT unknown import".
Every dependency I add is from the Maven Repository.
Here is the build.sbt file:
name := "server"
version := "1.0"
lazy val `server` = (project in file(".")).enablePlugins(PlayScala)
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
resolvers += "Akka Snapshot Repository" at "http://repo.akka.io/snapshots/"
resolvers := ("Atlassian Releases" at "https://maven.atlassian.com/public/") +: resolvers.value
scalaVersion := "2.12.2"
libraryDependencies ++= Seq(
jdbc,
ehcache,
ws,
specs2 % Test,
guice,
"org.mongodb.scala" %% "mongo-scala-driver" % "2.1.0",
"com.mohiva" %% "play-silhouette" % "5.0.2",
"com.mohiva" %% "play-silhouette-password-bcrypt" % "5.0.2",
"com.mohiva" %% "play-silhouette-crypto-jca" % "5.0.2",
"com.mohiva" %% "play-silhouette-persistence" % "5.0.2",
"com.mohiva" %% "play-silhouette-testkit" % "5.0.2" % "test"
)
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )

These dependencies were specified using a shorthand that is defined by the Play plugin:
jdbc,
ehcache,
ws,
specs2 % Test,
guice
So for them to work, be sure to include a line like the following in an .sbt file in the project directory:
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.10")
That should fix your problem.
Bonus tip: No need for the following lines, and because each additional resolver slows down SBT, you should delete them:
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
resolvers += "Akka Snapshot Repository" at "http://repo.akka.io/snapshots/"
resolvers := ("Atlassian Releases" at "https://maven.atlassian.com/public/") +: resolvers.value

Related

Sbt assembly for multiple targets

I need to create fat jars for multiple version of scala using sbt assembly.
When I target a single version, I write in simple.sbt:
scalaVersion := "2.11.12"
And the fat jar is output to target/scala-2.11/Kernalytics-assembly-1.0.jar. Now I would like to also target Scala 2.12. I could edit the sbt file to change scalaVersion, but I would like the assembly process to be automated over a range of versions of Scala when I call sbt assembly.
If I use crossScalaVersions:
name := "Kernalytics"
version := "1.0"
crossScalaVersions := Seq("2.11.12", "2.12.4")
libraryDependencies ++= Seq(
"org.scalanlp" %% "breeze" % "0.13.2",
"org.scalanlp" %% "breeze-natives" % "0.13.2",
"org.scalanlp" %% "breeze-viz" % "0.13.2"
)
libraryDependencies += "commons-io" % "commons-io" % "2.6"
resolvers += "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.4"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.4" % "test"
The only output is target/scala-2.12/Kernalytics-assembly-1.0.jar
If you use crossScalaVersions I think you need to prefix the command with a '+' if you want to build for all versions.
From Cross-Building a Project:
To build against all versions listed in crossScalaVersions, prefix the action to run with +

Libraries not resolving in sbt within intellij, but resolves and compiles from commandline

The below sbt file doesn't resolve the spark-xml databricks package from within intelliJ Idea where as works fine with command line
name := "dataframes"
version := "1.0"
scalaVersion := "2.11.8"
val sparkVersion="1.4.0"
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-core" % sparkVersion % "provided",
"org.apache.spark" %% "spark-sql" % sparkVersion,
"com.databricks" %% "spark-xml" % "0.3.3"
)
resolvers ++= Seq(
"Apache HBase" at "https://repository.apache.org/content/repositories/releases",
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
)
resolvers += Resolver.mavenLocal
The sbt was set to both bundled and other pointing to the locally installed sbt, but did not work either way.
The below package resolves and works perfectly from commandline
import com.databricks.spark._

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.

resolviing SBT dependencies

I am new to JVM development (I am using Scala and SBT) and am having trouble resolving dependencies. Yesterday, I had trouble resolving the org.restlet.2.1.1 dependency and today, I am having trouble with resolving the following:
[error] (*:update) sbt.ResolveException: unresolved dependency: com.mongodb.casbah#casbah_2.9.2;2.1.5-1: not found
[error] unresolved dependency: org.scalatra#scalatra_2.9.2;2.3.0: not found
[error] unresolved dependency: org.scalatra#scalatra-akka2_2.9.2;2.3.0: not found
[error] unresolved dependency: org.scalatra#scalatra-specs2_2.9.2;2.3.0: not found
I am using a giter8 scalatra-mongodb project template from github: click me. Since the project is a little old, it stands to reason that I am trying to obtain outdated versions that no longer exist or are compatible. What does one do in this situation? I tried fiddling with the version numbers in my build.sbt file, but this did not work (and appears to be worse!).
The following is the contents of my build.sbt file:
scalaVersion := "2.9.2"
mainClass := Some("JettyLauncher")
seq(webSettings :_*)
port in container.Configuration := 8080
seq(assemblySettings: _*)
libraryDependencies ++= Seq(
"com.mongodb.casbah" %% "casbah" % "2.8.1-1",
"org.scalatra" %% "scalatra" % "2.2.0",
"org.scalatra" %% "scalatra-akka2" % "2.2.0",
"org.scalatra" %% "scalatra-specs2" % "2.2.0" % "test",
"org.mortbay.jetty" % "servlet-api" % "3.0.20100224" % "provided",
"org.eclipse.jetty" % "jetty-server" % "8.0.0.M3" % "container, compile",
"org.eclipse.jetty" % "jetty-util" % "8.0.0.M3" % "container, compile",
"org.eclipse.jetty" % "jetty-webapp" % "8.0.0.M3" % "container, compile"
)
resolvers ++= Seq(
"Sonatype OSS" at "http://oss.sonatype.org/content/repositories/releases/",
"Sonatype OSS Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/",
"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
"Akka Repo" at "http://akka.io/repository/",
"Web plugin repo" at "http://siasia.github.com/maven2"
)
The following is my plugins.sbt file:
addSbtPlugin("com.earldouglas" %% "xsbt-web-plugin" % "0.9.0")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.7.2")
Note that when I first generated the template, I was receiving missing dependencies for this first plugin. Fortunately, the github page for this plugin gave updated instructions and I am able to get past this dependency.
Anyway, what are the versions of these dependencies that I need to get everything working? In general what is a strategy for resolving these dependencies (right now I have no idea what to do (other than visit the github pages and fiddle with version numbers)?
Thanks for all the help!
I have at least gotten sbt to resolve my dependencies for whatever that is worth (it has classpath issues now...). Anyway, the following is my new and improved build.sbt file:
scalaVersion := "2.10.4"
mainClass := Some("JettyLauncher")
seq(webSettings :_*)
port in container.Configuration := 8080
seq(assemblySettings: _*)
libraryDependencies += "org.mongodb" %% "casbah-core" % "2.7.3"
libraryDependencies += "org.scalatra" %% "scalatra" % "2.2.0-RC3" cross CrossVersion.binary
libraryDependencies += "org.scalatra" %% "scalatra-akka" % "2.2.0-RC3"
libraryDependencies += "org.scalatra" %% "scalatra-specs2" % "2.2.0" % "test"
libraryDependencies += "org.mortbay.jetty" % "servlet-api" % "3.0.20100224" % "provided"
libraryDependencies += "org.eclipse.jetty" % "jetty-server" % "9.0.0.M5" % "container"
libraryDependencies += "org.eclipse.jetty" % "jetty-util" % "9.0.0.M5" % "container"
libraryDependencies += "org.eclipse.jetty" % "jetty-webapp" % "9.0.0.M5" % "container"
resolvers ++= Seq(
"Sonatype releases" at "http://oss.sonatype.org/content/repositories/releases/",
"Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/",
"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
"Akka Repo" at "http://akka.io/repository/",
"Web plugin repo" at "http://siasia.github.com/maven2"
)
All I really did was spend a few hours googling and searching mvn repositories that were compatible with the scala version I am using (2.10.4). I learned that %% will append the scala-version to the dependency name (seems like a nice convention since Scala is always evolving). Once, I got a few dependencies resolved, the rest caved!

Can't resolve casbah as a sbt dependency

I'm getting the following error upon compile:
[error] (*:update) sbt.ResolveException: unresolved dependency: org.mongodb#casbah_2.11;2.7.4-SNAPSHOT: not found
With this build.sbt file:
name := """play-scala"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
ws
)
libraryDependencies += "org.mongodb" %% "casbah" % "2.7.4-SNAPSHOT"
// For stable releases
resolvers += "Sonatype releases" at "https://oss.sonatype.org/content/repositories/releases"
// For SNAPSHOT releases
resolvers += "Sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
I'm using Typesafe Activator and have tried many combinations of Casbah versions (including releases, instead of snapshots) and resolvers. The installation instructions from the "official" MongoDB Casbah tutorial don't work either. Any ideas?
If you go to the release and snapshot URLs and try to manually locate casbah, you'll see that the snapshot url doesn't have anything for casbah currently.
On the other hand, the release repo has 2.7.3 for Scala 2.11. Check it out here.
Maybe try
libraryDependencies += "org.mongodb" %% "casbah" % "2.7.3"
use following dependancy in your build.sbt file :
libraryDependencies += "org.mongodb" %% "casbah" % "2.8.1"
use these lib
"org.mongodb" %% "casbah" % "3.1.1",
"org.json4s" %% "json4s-jackson" % "3.5.2",
"org.json4s" %% "json4s-mongo" % "3.5.2"