I'm relatively new to Scala. I'd like to load all project dependencies into Scala shell interpreter in order to test somme coding. Is there any trick to simply do it?
Here is my build.sbt. Somme of those dependencies call for more then 38 other dependencies. Can't collect jars manually.
libraryDependencies += "org.apache.cassandra" % "cassandra-all" % "2.2.4"
libraryDependencies += "junit" % "junit" % "4.11"
Thanks in advance.
Saïd
You can get a REPL with all dependencies of you build.sbt by calling sbt console in the directory containing the build.sbt.
Buildfile: build.sbt
name := """example"""
scalaVersion := "2.11.7"
libraryDependencies += "biz.neumann" % "nice-uuid_2.11" % "1.1"
REPL with lib usage example
Starting
$ sbt console
in the REPL
...
[info] Starting scala interpreter...
scala> import biz.neumann.NiceUUID._
import biz.neumann.NiceUUID._
scala> "9ecce884-47fe-4ba4-a1bb-1a3d71ed6530".uuid
res0: scala.util.Try[java.util.UUID] = Success(9ecce884-47fe-4ba4-a1bb-1a3d71ed6530)
Related
As I want to execute the jar generated by my scala project in the Command Line Interface, I get the following problem:
Exception in thread "main" java.lang.NoClassDefFoundError: org/rogach/scallop/ScallopConf
Although in the dependency file I mentionned scallop dependency as follow
import sbt._
object Dependencies {
lazy val betterFiles = "com.github.pathikrit" %% "better-files" % "3.7.0"
lazy val scalaz = "org.scalaz" %% "scalaz-core" % "7.2.27"
lazy val scallop = "org.rogach" %% "scallop" % "3.1.5"
// -- Logging
lazy val scalaLogging = "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
lazy val slf4jBackend = "org.slf4j" % "slf4j-simple" % "1.7.26"
// -- Testing
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.5"
}
My build.sbt file is the following:
import Dependencies._
ThisBuild / scalaVersion := "2.12.5"
ThisBuild / sbtVersion := "1.2.6"
ThisBuild / version := "0.1.0-SNAPSHOT"
lazy val root = (project in file("."))
.settings(
name := "phenix-challenge",
libraryDependencies ++= Seq(
betterFiles,
scalaz,
scallop,
scalaLogging,
slf4jBackend % Runtime,
scalaTest % Test
)
)
If you have an Idea that could resolve my Issue please HELP!
Many thanks in advance
To execute the jar generated by your scala project in the Command Line Interface you can use sbt plugin to assembly a fat-jar including your libraries/dependencies. Having such jar you would be able to run your app via java -jar ...
There are several SBT plugins for build a fat-jar. Perhaps the easiest one would be the sbt-assembly.
Add this plugin to file project/plugins.sbt (create this file if needed):
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.9")
Now use sbt to build a fat-jar:
sbt assembly
Then run via java -jar YouMainClass
Another option is to use pure sbt to run Main class using command sbt run, then you do really need to build a fat-jar.
I am trying to learn the basics of Scala, scalatest, and sbt and I'm following a tutorial. This is my built.sbt file:
name := "demo-hello"
version := "0.1"
scalaVersion := "2.12.6"
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % "test"
I have a test that looks like this (showing this is probably unnecessary:
package demo
import org.scalatest.FunSuite
class HelloTest extends FunSuite {
test("say hello method works correctly") {
val hello = new Hello
assert(hello.sayHello("Scala") == "Hello, Scala!")
}
}
What should I do from here? I am trying to run the test but I get this error:
Error:(8, 36) can't expand macros compiled by previous versions of Scala
assert(hello.sayHello("Scala") == "Hello, Scala!")
I'm not that familiar with the % symbol btw.
FIX
I changed my build.sbt to this:
name := "demo-hello"
version := "0.1"
scalaVersion := "2.10"
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % "test"
Remaining questions:
So it seems downgrading to scalaVersion "2.10" worked. Why?
What is an artifact? scalatest is apparently an artifact?
Where is scalaversion 2.10 kept on my machine? It seems I only have scala 2.12. Where in my project folder is version 2.10?
Answering your questions slightly out-of-order:
2 - An "artifact" is something that's built by maven, sbt, or another build system. For Scala or Java, this is almost always a jar file. Each item in libraryDependencies specifies a file in a maven repository (a database of artifacts).
1 - Scala class files are not compatible between minor versions of Scala. When you download a Scala jar from a maven repository, the Scala version is specified as part of the artifact name. The _2.10 in your dependency declares that you wish to use the version of scalatest that's compile for Scala 2.10 - which is why you were getting an error using it in your Scala 2.12 application.
When declaring dependencies on Scala artifacts in sbt, you should always use the %% operator, which automatically appends the appropriate suffix to your artifact, like so:
// This works for any scalaVersion setting.
libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.0" % "test"
3 - sbt handles downloading the appropriate runtime files for the declared version of Scala automatically.
I tried importing JSON libraries in sbt, so that my custom sbt task can write a json file using a JSON api. However it seems like sbt can't import those libraries, but rather it can only import "standard" libraries like scala.io.Source, java.io.File, etc...
Both commented out lines below would each fail sbt:
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.7"
libraryDependencies += "io.argonaut" %% "argonaut" % "6.0.4"
compile in Compile <<= (compile in Compile) map { c =>
import scala.io.Source
//import play.api.libs.json.Json
//import argonaut._, Argonaut._
What might it be? Must I write a plugin to circumvent this?
$ about
[info] This is sbt 0.13.6
[info] The current project is built against Scala 2.11.6
[info] Available Plugins: ...
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
Of course I can just string interpolate my json bare-handed but I wonder what might it be...
Thanks!
According to this snippet from the plugin documentation, you just need to include the dependency in plugins.sbt instead of your build definition (ie., no plugin is required).
// [within plugins.sbt]
// plain library (not an sbt plugin) for use in the build definition
libraryDependencies += "org.example" % "utilities" % "1.3"
So you should be able to just add these to plugins.sbt:
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.3.7"
libraryDependencies += "io.argonaut" %% "argonaut" % "6.0.4"
In our project, I would like access to what is a provided dependency at runtime in the sbt interactive console. It stops one from being forced to deploy to a Storm cluster each time they want to test something. Are there any options on the 'run' task to accomplish this?
I think I got this working.
First, I created a build.sbt as normal.
Contents of build.sbt in project root directory:
name := "Test"
version := "1.0"
scalaVersion := "2.10.1"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "com.typesafe.akka" % "akka-actor_2.10" % "2.1.2"
libraryDependencies += "com.typesafe.akka" % "akka-kernel_2.10" % "2.1.2"
libraryDependencies += "com.typesafe.akka" % "akka-remote_2.10" % "2.1.2"
libraryDependencies += "org.jsoup" % "jsoup" % "1.7.2"
Then I ran "sbt console". This downloaded project dependencies specified in the build.sbt (download output not shown) and starts a Scala shell with the dependencies included on the path.
cortland:jjasinski$ sbt console
[info] Set current project to Jazstudios Bot (in build
file:/Users/jjasinski/Documents/crawler/myproject/)
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import org.jsoup
import org.jsoup
I found the information here
http://www.scala-sbt.org/0.12.3/docs/Howto/scala.html
I am trying to install sbt-assembly by following the instructions in order to make a stand-alone jar that can run on a computer without scala installed.
So far these are the steps I've taken.
I created a plugins.sbt file:
$ cat sbt/project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.7.2")
And I added the following to the beginning of my build.sbt file:
$ head -n3 sbt/build.sbt
import AssemblyKeys._ // put this at the top of the file
seq(assemblySettings: _*)
But when I run sbt, I get the following error:
sbt/build.sbt:1: error: not found: value AssemblyKeys
import AssemblyKeys._
Make sure you are running sbt version at least 0.11 by typing
$ sbt sbt-version
at the bash prompt.
Make sure you have the plugins file set up as follows:
$ cat sbt/project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.7.2")
Make your build file (build.sbt) look like this:
import AssemblyKeys._
seq(assemblySettings: _*)
name := "my_project"
version := "1.0"
scalaVersion := "2.9.1"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "1.6.1" % "test",
"commons-lang" % "commons-lang" % "2.6"
)
traceLevel in run := 0
fork in run := true
scalacOptions ++= Seq("-optimize")
// The following is the class that will run when the jar is compiled!
mainClass in assembly := Some("MyMain")
Make sure you don't have a project/plugins folder lying around. This may prevent other mechanisms of specifying plugins from working.
You shouldn't import plugin settings into build.sbt (basic configuration): 1) build.sbt is not a normal Scala source file 2) plugin settings are pre-imported.
So you simply should do
seq(assemblySettings: _*)
Imports are required only when you use full/extended build configuration.