Can't use imports inside sbt build definition code - scala

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"

Related

object mockito is not a member of package org

Relatively new to sbt and Mockito.
I want to use Mockito in tests, but I'm getting errors related to the Mockito imports when I compile the tests
Imports in test file:
import org.scalatest._
import org.mockito.Mockito._
import org.scalatest.mockito.MockitoSugar
sbt file:
name := "blah"
version := "0.1"
scalaVersion := "2.13.0"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
libraryDependencies += "org.mockito" % "mockito-core" % "1.8.5" % "test"
I get these error messages when the tests (fail to) compile:
object mockito is not a member of package org [error] import org.mockito.Mockito._
and also:
Symbol 'type org.mockito.MockSettings' is missing from the classpath.
[error] This symbol is required by 'value org.scalatest.mockito.MockitoSugar.mockSettings'.
I've had a play around with changing some of the versions of scalatest and mockito in the sbt file, but not really if that's getting at the root of the problem or not.
Thanks for any help!
You're using a very old version of Mockito, which is older than the one Scalates relies on, you probably need some 2.x.x version.
On the other hand, I'd recomend you to go fo mockito-scala rather than mockito-core and skip the Scalatest provided classes altogether as they are quite basic.
I suspect you have a caching problem. This happens especially with Intellij.
Here 2 ideas:
Reload the sbt project. See https://stackoverflow.com/a/20466144/2750966
Close the project / delete .idea an open the project newly with Intellij.
Let me know if it is not related with Intellij

Intellij Scala classpath not found

I am trying to use json4s but I keep getting the error below when I compile. I thought the library would have the class internally. I am using json4s-ast_2.11-4.0.0-M1.jar, json4s-core_2.12.3.5.3.jar and json4s-jackson_2.9.1-3.0.0.jar.
Error:(64, 25) Symbol 'type org.json4s.JsonAST.JValue' is missing from the classpath.
This symbol is required by 'type org.json4s.JValue'.
Make sure that type JValue is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
A full rebuild may help if 'package.class' was compiled against an incompatible version of org.json4s.JsonAST.
Imports:
import org.json4s.jackson.JsonMethods._
import org.json4s._
build.sbt
scalaVersion := "2.12.4"
libraryDependencies += "org.json4s" %% "json4s-jackson" % "3.0.0"
libraryDependencies += "org.json4s" %% "json4s-ast" % "4.0.0-M1"
libraryDependencies += "org.json4s" %% "json4s-core" % "3.5.3"
You can't use libraries compiled for different versions of scala in other versions. You need to fix the versioning of your JSON4s. Look at:
http://www.scala-sbt.org/0.13/docs/Cross-Build.html#Using+Cross-Built+Libraries

How to write the "import scala.io.Source" , "import java.io" libraries in SBT

I am compiling the Scala program using the SBT but It gives me following error for "import scala.io.Source" , "import java.io"
sbt.ResolveException: unresolved dependency: org.scala#scala.io.Source_2.11;latest.integration: not found
[error] unresolved dependency: org.java#java.io_2.11;latest.integration: not found
My SBT format is given below:
name := "Simple Project"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"org.apache.spark" %% "spark-graphx" % "2.0.1",
"org.scala" %% "scala.io.Source" % "latest.integration",
"org.java" %% "java.io" % "latest.integration"
)
Anybody can help that how i can specify the "import scala.io.Source" , "import java.io" in SBT.
One needs to make a distinction between library dependencies and package imports: library dependencies are managed through the build system (like sbt or maven or grails,...) and make complete libraries (like a logging API, HTTP implementation, ...) available to the system being built.
At the program level, imports are used to bring specific parts of libraries into the scope of the code being developed.
Given this build.sbt
name := "examplebuild"
version := "0.0.1"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.2.1",
"org.scalaj" % "scalaj-http_2.11" % "2.3.0"
)
We can develop a scala program that can use the configuration library from typesafe and the http library from scalaj
Sample.scala
package com.example
import scala.io.Source // from the Scala standard library
import java.io._ // import all io package from the standard java library
import com.typesafe.ConfigFactory // from the typesafe config library
import scalaj.http._ // import all members of the scalaj.http package
class Sample {
// code here
}

Scala shell interpreter with project dependencies

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)

Why does sbt give "object scalacheck is not a member of package org" after successful scalacheck resolve?

I have added the following in build.sbt:
libraryDependencies <<= scalaVersion { scala_version => Seq(
<other entries>
"org.scalacheck" %% "scalacheck" % "1.10.0" % "test",
<other entries>
)
}
Upon compile the project in sbt, the dependencies are resolved successfully as can be seen in the logs:
[info] Resolving org.scalacheck#scalacheck_2.9.1;1.10.0 ...
...
Done updating
However, I get the following error during compilation of one file.
object scalacheck is not a member of package org
import org.scalacheck.Gen
^
What can be the reason for this?
Is there a way to see the classpath that sbt is using during the compile task?
Sbt version: 0.11.2
OS: Windows 7
Scala version: 2.9.1
Note that the project builds fine in ScalaIDE. (I use sbteclipse to generate the eclipse .classpath file. The generated .classpath has proper scalacheck entry.)
The way you import the dependency makes Scalacheck only available for the command test:
"org.scalacheck" %% "scalacheck" % "1.10.0" % "test"
You should use:
"org.scalacheck" %% "scalacheck" % "1.10.0"
But why do you use Scalacheck somewhere else than in tests ? See this link for more explanations about testing in sbt.