how to use CSPARQL-ReadyToGoPack-0.9 in a SBT project - scala

I want to use the CSPARQL-ReadyToGoPack-0.9 in an SBT project. I'm on Linux. How can I do that?

In your build script you probably need to specify an artifact explicitly for the dependency (the module id parameters are jus):
import sbt._
import Keys._
libraryDependencies +=
"foo" % "bar" % "0.1" artifacts Artifact("foo", url("file:/path/to/jar"))

Related

Unable to stub controller components

The method stubControllerComponents in package play.api.test appears to use same package and object name as a separate dependency which is causing a conflict when I attempt to use stubControllerComponents :
play.api.test.Helpers.stubControllerComponents is not found in below code:
import java.io.File
import play.api.test
import play.api.mvc._
import javax.inject._
import play.api.Environment
import play.api.mvc.{AbstractController, ControllerComponents}
class CountController #Inject() (cc: ControllerComponents,
env: Environment) extends AbstractController(cc) {
def getter() = Option(env.classLoader.getResourceAsStream("file.csv"))
}
play.api.Environment(play.api.test.Helpers.stubControllerComponents, Environment.simple())
This Helpers contains the method I require stubControllerComponents :
But this version of the class is being imported with import play.api.test :
Play link for stubbing : https://www.playframework.com/documentation/2.6.x/Highlights26#StubControllerComponents
build.sbt:
name := "ddd"
version := "1.0"
lazy val `ddd` = (project in file(".")).enablePlugins(PlayScala)
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
resolvers += "Akka Snapshot Repository" at "https://repo.akka.io/snapshots/"
scalaVersion := "2.12.2"
libraryDependencies ++= Seq( jdbc , ehcache , ws , guice , specs2 % Test)
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
Do I need to exclude portions of a dependency, in this case filters-helpers in order to make stubControllerComponents available ?
Update:
play.api.test.Helpers.stubControllerComponents not found:
Update2:
You seem to be using a scratch file. AFAICS, there is no way to also include dependencies from your Test scope into the classpath of your worksheet.
A workaround would be to (temporarily) add the play-test artefact to your libraryDependencies. Or just create a proper test file, which has access to the Test libraries normally.
play.api.test.Helpers.stubControllerComponents is provided by play-test
dependency
libraryDependencies += "com.typesafe.play" %% "play-test" % PlayVersion.current % "test",
which is indirectly imported by Play's sbt plugin specified at project/plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.1")
after explicitly enablePlugins(PlayScala) within project's build.sbt.
Note how play-test is out-of-the-box scoped to test configuration hence it is provided only on the test classpath. If you wish to reference stubControllerComponents from within IntelliJ Scala Worksheet, then make sure to create the worksheet inside test/ directory and not inside app/ directory. This will make Scala Worksheet use test classpath.

How to correctly import SBT Build.scala in scala-ide

As an sbt build can be written in scala and is itself a scala project, i would like to import it in scala-ide as a scala project. For example with the following code.
Build.scala
import sbt._
import Keys._
object TestBuild extends Build {
lazy val root = Project(id = "test",
base = file("."),
settings = Seq(
organization := "com.tomahna",
name := "demo",
scalaVersion := "2.11.8"))
}
plugins.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")
This build works fine with sbt, however Build.scala is not compiled by eclipse, thus i get neither compilation errors nor auto-completion.
I can add the project folder to source folders but then import sbt._ and import Keys._ will fail because the eclipse project is not correctly set to provide these dependencies.
Is there a way to setup the sbt project so that it interact nicely with scala-IDE ?
From sbteclipse manual:link
If you want to get Eclipse support for the sbt build definition, e.g. for your Build.scala file, follow these steps:
If you are not using sbteclipse as as global plugin, which is the recommended way, but as a local plugin for your project, you first have to add sbteclipse as a plugin (addSbtPlugin(...)) to the build definition project, i.e. to project/project/plugins.sbt
In the sbt session, execute reload plugins
Set the name of the build definition project to something meaningful: set name := "sbt-build"
Execute eclipse and then reload return
Import the build definition project into Eclipse and add the root directory to the build path

Sbt test file cannot find value from src file

My sbt project with scala-test is layout as standard:
sbt-example
build.sbt
src/main/scala/local/search/BinarySearch.scala
src/main/scala/local/util/Utility.scala
src/test/scala/local/search/SearchingSuite.scala
My build.sbt is very simple:
name := "helloworld"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.4"
)
My BinarySearch.scala looks like:
package local.search
object BinarySearch {
// def ...
}
My SearchingSuite.scala looks like:
package local.search
import org.scalatest.FunSuite
class SearchingSuite extends FunSuite{
// call functions from BinarySearch object
}
Run commands are:
sbt compile
sbt "test-only local.search.SearchingSuite"
Then sbt raises error:
not found: value BinarySearch
What's wrong with my code? And if I need to use some object from Utility.scala, how to import in SearchingSuite.scala?
Thanks, will vote up with any answers!
Let's say you are working on class A; and class A requires class B. As long as A and B belong to two different packages, you have to import class B.
In your case, SearchingSuite and BinarySearch belong to two different packages. In particular, the former belongs to the main source package, the latter belong to the test source package. Therefore, you must import BinarySearch in SearchingSuite. Otherwise, there will be an compilation error like what you have encountered.
To use some object from Utility, you
import local.util.Utility
if Utility is a class, or
import local.util.Utility._
if Utility is an object.
If you are using some IDE, like Intellij or Eclipse, right click on the error highlight, the IDE will pop up some suggestions.

Importing .jar files into Scala environment

Even after reading: Scala, problem with a jar file, I'm still a bit confused. I am trying to import some packages into my Scala file, and the interpreter is not recognizing them even after adding to classpath.
One example:
I have the import statement:
import org.json4s._
I downloaded the .jar from here: http://mvnrepository.com/artifact/org.json4s/json4s-native_2.10/3.2.4
and added to the interpreter classpath using:
scala> :cp /Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar
Scala acknowledges the classpath:
Your new classpath is: ".:/Users/aspangher13/Downloads/json4s-native_2.10-3.2.4.jar:/Users/aspangher13/Downloads/jna-3.5.2.jar"
But still throws this error:
<console>:7: error: object scalatra is not a member of package org
import org.json4s._
Can anyone see what I'm doing wrong? Thanks!!
And as a followup, does anyone know where to find the package: JsonAST._?
Go the simple and create a little sbt project.
First step - create a project
For your purposes you don't need a complex build. So just create two files:
./build.sbt
name := "name your project"
version := "0.1"
scalaVersion := "2.10.2" // or whatever you prefer
./project/build.properties
sbt.version=0.12.4
The just go to the project root folder and call sbt
Second step - add dependencies
Open your ./build.sbt file and add:
libraryDependency ++= Seq(
"org.scalatra" %% "scalatra" % "2.2.1",
"org.scalatra" %% "scalatra-scalate" % "2.2.1",
"org.scalatra" %% "scalatra-specs2" % "2.2.1" % "test",
"org.json4s" %% "json4s-native % "3.2.4",
"net.java.dev.jna" & "jna" & "3.5.2"
)
Step three - run the console
Don't forget to reload sbt with reload task, and then call console or console-quick task. This should work.
But there are easier ways to do this:
1) Use gitter8 - Scalatra gitter8 project
2) Read little into about Scalatra sbt dependencies
Still not sure how :cp works but if you execute
scala -classpath "list of jars colon separated"
then inside the REPL do your imports it should work
import org.json4s._
import org.xyz
However, when you try to use the classes you are likely to be missing transitive dependencies required by json4s and so we come back to the sbt example # 4lex1v describes, which will handle this. Creating a little project and running sbt console will indeed greatly simplify this.
Seems like the -classpath and :cp are primarily meant to make your code available in the shell and then only if you understand all of the transitive dependencies, or have none.

SBT can't resolve dependency in build definition

I'm making an SBT task that needs to make a multipart POST request to a certain server. I want to use Dispatch to make the request. I have the following in build.sbt at the top level of my project:
libraryDependencies ++= Seq(
"net.databinder.dispatch" %% "dispatch-core" % "0.9.5"
)
The task definition is in project/Build.scala. I have
import sbt._
import Keys._
import dispatch._
object SubmitBuild extends Build {
...
}
I get the following error message:
[error] /Users/ken/xxxxtools/project/Build.scala:3: not found: object dispatch
[error] import dispatch._
[error] ^
If I remove import dispatch._ then sbt will compile. I know I have Dispatch installed. Why can't SBT find it?
If you want to make references in Build.scala to some dependency it has to be declared in build's project not in the "project project". Meaning that it should be project/build.sbt.
It turns out that project/Build.scala is also a SBT project in the same way your project is.
SBT authors give a very good explanation in sbt is recursive.