scala module after sbt installation - scala

May I ask some newbie question here:
My goal is to use Breeze module (0.10) in Scala (2.11.5), and I follow:
$ sbt
set scalaVersion := "2.10.3" // or 2.11.3
set libraryDependencies += "org.scalanlp" %% "breeze" % "0.10"
set resolvers += "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
console
and actually, it ran well, and at the end, when I got the prompt I can do this well (some example from the Breeze site)
import breeze.linalg._
However, after I exit and re-enter Scala by just
$ scala
Welcome to Scala version 2.11.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_75).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
and with some excitement, I tried:
scala> import breeze.linalg._
<console>:7: error: not found: value breeze
import breeze.linalg._
^
It seems the module is "gone" after the installation ...
May I know how to get "Breeze" installed permanently? or did I miss any linkage here?
Your answer is much appreciated.

The scala/java ecosystem just doesn't work that way; the global classpath doesn't get changed because some project uses a jar.
Your easiest solution is to just make a bash alias (e.g. brscala) that invokes the same sbt incantation.
There are ways to make it work (involving putting symlinks to breeze and all of its dependencies in $SCALA_HOME/lib), but that's definitely not recommended.

Looks like you want to access the libraries from the Terminal
Specify the dependencies (here breeze & saddle) in build.sbt, e.g.
scalaVersion := "2.10.4"
resolvers ++= Seq(
"Sonatype OSS Releases" at "http://oss.sonatype.org/content/repositories/releases/",
"Sonatype OSS Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/",
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
"Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
)
libraryDependencies ++= Seq(
"joda-time" % "joda-time" % "2.1",
"org.joda" % "joda-convert" % "1.2",
"org.scala-saddle" %% "saddle-core" % "1.3.+",
"org.specs2" %% "specs2" % "2.4.1" % "test",
"org.scalacheck" %% "scalacheck" % "1.11.3" % "test",
"junit" % "junit" % "4.11" % "test",
"org.scalanlp" %% "breeze" % "0.12",
"org.scalanlp" %% "breeze-natives" % "0.12",
"org.scalanlp" %% "breeze-viz" % "0.12"
)
initialCommands := """
import org.joda.time.DateTime
import org.saddle._
import org.saddle.time._
import org.saddle.io._
import me.first_install_saddle._
import breeze.linalg._"""
Assuming you are cd into the directory of build.sbt: run sbt update to install the dependencies
Then instead of just scala, run sbt console in Terminal
From there you can access the libraries from the Terminal.

Related

Manage dependencies with sbt & IntelliJ IDEA

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

Issue importing Akka packages

I have the following build.sbt file:
name := "akkaHttp"
version := "1.0"
scalaVersion := "2.12.0"
resolvers ++= Seq("Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
Resolver.bintrayRepo("hseeberger", "maven"))
libraryDependencies ++= {
val AkkaVersion = "2.3.9"
val AkkaHttpVersion = "2.0.1"
val Json4sVersion = "3.2.11"
Seq(
"com.typesafe.akka" %% "akka-slf4j" % AkkaVersion,
"com.typesafe.akka" %% "akka-http-experimental" % AkkaHttpVersion,
"ch.qos.logback" % "logback-classic" % "1.1.2",
"org.json4s" %% "json4s-native" % Json4sVersion,
"org.json4s" %% "json4s-ext" % Json4sVersion,
"de.heikoseeberger" %% "akka-http-json4s" % "1.4.2"
)
}
In it, these dependencies are not met.
Error:Unresolved dependencies:
com.typesafe.akka#akka-slf4j_2.12;2.3.9: not found
com.typesafe.akka#akka-http-experimental_2.12;2.0.1: not found
org.json4s#json4s-native_2.12;3.2.11: not found
org.json4s#json4s-ext_2.12;3.2.11: not found
de.heikoseeberger#akka-http-json4s_2.12;1.4.2: not found
So what should I add to it such that the imports work?
Notice the _2.12 appended to the artifacts? Based on your scalaVersion, SBT tried to download the dependencies built against Scala 2.12.x, but couldn't find any. Try using Scala version 2.11.8.
Different versions of Scala can be binary incompatible, so libraries are cross built against those different versions and published to the repositories. Looks like it hasn't yet happened for those libraries above.
Note that Scala 2.12 is indeed not binary compatible with Scala 2.11.

Play project compiles successfully but IDEA cannot resolve symbols while importing dependencies

Piece of build.sbt related to Play! application project.
resolvers += Resolver.jcenterRepo,
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases",
libraryDependencies ++= Seq(
"com.kyleu" %% "jdub-async" % "1.0",
"com.vmunier" %% "play-scalajs-scripts" % "0.3.0",
"org.webjars" % "jquery" % "1.11.1",
"org.json4s" %% "json4s-jackson" % "3.2.11",
evolutions,
"com.github.benhutchison" %% "prickle" % "1.1.7",
specs2 % Test
)
IDEA said to me that it cannot resolve symbol jdub when I import jdub-async library. Meanwhile project is compiling successfully.
How is it possible to fix this bug?
This is just an issue with IntelliJ IDEA's own Scala compiler. It chokes on many things. If it compiles fine with SBT itself, you don't have any bug to resolve (unless you want to report it to IntelliJ).

How do I add scalatest dependency to an existing scala/lift project?

I am trying to add scalatest support to an existing scala and lift app
I add a dependency to build.sbt as per the following excerpt
resolvers ++= Seq(
"Java.net Maven2 Repository" at "http://download.java.net/maven/2/",
"Sonatype scala-tools repo" at "https://oss.sonatype.org/content/groups/scala-tools/",
"Sonatype scala-tools releases" at "https://oss.sonatype.org/content/repositories/releases",
"Sonatype scala-tools snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
)
libraryDependencies ++= {
val liftVersion = "2.6"
Seq(
"net.liftweb" %% "lift-webkit" % liftVersion % "compile",
"net.liftweb" %% "lift-mapper" % liftVersion % "compile",
"junit" % "junit" % "4.7" % "test",
"org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
)
}
and then I create a test class
class GeoUtilsTest extends org.scalatest.FlatSpec {
}
when I do sbt compile or sbt test I get the following error
[error] C:\Users\Andrew Bucknell\Documents\project-alpha\src\main\scala\com\myapp\api\utilities\GeoUtils$Test.scala:6: object scalatest is not a member of package org
[error] class GeoUtilsTest extends org.scalatest.FlatSpec {
[error] ^
The problem here is that scalatest cannot be found. I am not sure why adding the dependency to the build.sbt isnt enough to bring it in and add it to the project - its always been enough before. sbt update finds and downloads the dependency. Any thoughts?
It seems that you have specified the dependency of scalatest to be visible only to test package and you code doesn't seems to be located there. So, either change the dependency into build.sbt from test to compile, or better move the testing code into test package.

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"