how to use JavaCV from SBT (Simple Build Tool) in Scala? I need to use JavaCV so I can write a Scala application using this.
Simple Build Tool is here: http://www.scala-sbt.org/
JavaCV is here: http://code.google.com/p/javacv/
Add the following to your build.sbt file.
resolvers += "JavaCV maven repo" at "http://maven2.javacv.googlecode.com/git/"
libraryDepedencies += "com.googlecode.javacv" % "javacv" % "0.2"
This should pull down javacv for you, as well as any dependencies. Note you will still need to have all the native libraries set up like javacv details, this only gets the jars for your project.
I created an SBT plugin that solves most of this for you in 1 line; no need to set up native libraries etc because this will do it all for you.
https://github.com/lloydmeta/sbt-opencv
Usage:
Add the following in project/plugins.sbt:
addSbtPlugin("com.beachape" % "sbt-opencv" % "1.2")
Related
I am following a tutorial to perform object detection in scala. I am
having issues adding the tensorFlow dependency. I have followed the instructions on the official Tensorflow for Scala website http://platanios.org/tensorflow_scala/installation.html, but that doesn't seem to work. I also made sure to use the Java 11 JDK for the project. However, whenever I try to add the sbt dependency
libraryDependencies += "org.platanios" % "tensorflow" % "0.4.0" classifier "linux-cpu-x86_64", I get a "No dependencies found for given import" error in IntelliJ. Any idea on how to set this up properly ?
Try to replace one % in your dependency line to twice %%:
libraryDependencies += "org.platanios" %% "tensorflow" % "0.4.0" classifier "linux-cpu-x86_64"
On top of what the previous answer already suggested, I believe it's probably worth mentioning that (until 2.12) libraries in the 2.x are not binary-compatible across versions. The convention for Scala libraries is to append a _2.x to the published library JAR's artifact identifier. Since SBT was built around Scala (and it's its de facto standard build tool) it acknowledges this conventions and the %% operator will automatically append that extra "qualifier" based on the Scala version you are using.
Notice here on mvnrepository.com how the artifact identifier changes between the Maven and the SBT dependency declaration (in Maven, the artifact identifier is tensorflow_2.12, in SBT the %% allows you to not have to specify that).
The single % is generally used for Java dependencies (that are not affected by the aforementioned convention).
As an alternative (that I would suggest just to play around and see that there's no magic involved), you can also use % to specify a Scala dependency and explicitly mention the Scala version in the artifact identifier, as follows:
libraryDependencies += "org.platanios" % "tensorflow_2.12" % "0.4.0" classifier "linux-cpu-x86_64"
The good news is that starting from Scala 2.13 this issue was tackled at the very root using an intermediate representation that was also introduced to make sure the interoperability between Scala 2.13 and Scala 3.x compiled code.
EDIT
What you have found was actually an issue in the documentation that was already reported, I opened a PR to fix it.
I want to enable Scala Test
by first adding this line to ~/.sbt/0.13/global.sbt:
resolvers += "Artima Maven Repository" at "http://repo.artima.com/releases"
How to do this with IJ?
In order to enable the Artima SuperSafe Compiler Plugin, you do need to enable an additional resolver. This is distinct from ScalaTest, which is just the testing library. The compiler plugin offers the following features from the site:
Artima SuperSafe is a commercial Scala compiler plugin with a free Community Edition that checks ScalaTest/Scalactic === and matcher expressions for correctness.
IntelliJ's sbt settings are located at Build, Execution, Deployment > Build Tools > sbt
Unless you have a very bespoke installation, the bundled sbt will read from
~/.sbt/1.0 for global plugins and other settings. So if you add or create a file in that directory (i.e. ~/.sbt/1.0/global.sbt and add the following line to that file:
resolvers += "Artima Maven Repository" at "http://repo.artima.com/release
You should be able to use the plugin you added to your project/plugins.sbt file for your specific project, which probably looks something like this:
addSbtPlugin("com.artima.supersafe" % "sbtplugin" % "1.1.3")
You may need to restart the sbt shell to have those settings take effect. Hopefully that should help.
I am using a library say A in Scala which is dependent on version x.11 of another library say Z.
Now, I am also using a library say B which is dependent on version x.31 of Z.
This leads to compile error because we will have two versions of library Z, how can I use both libraries A and B in scala's sbt? Is there any way to specify it.
If completely replacing one dependency with a newer version happens to work, then Sparko's solution works. However, that isn't always the case.
If you want to include both versions of a library in the uber-jar produced by sbt-assembly, you'll need to use shading. See this post for an overview of what shading is, and some of the drawbacks associated with it.
Shading is covered in sbt-assembly's documentation here, but if you're anything like me, their way of explaining it will leave you more confused than you started. There's a good blog post, Spark, Uber Jars and Shading with sbt-assembly (waybackmachine link), that helps to demystify it a bit. Here's the relevant section:
I can shade over my typesafe config version, giving it a different
name so Spark won’t get confused between the versions. I quickly went
to my build.sbt file, and added the following code:
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("com.typesafe.config.**" -> "my_conf.#1")
.inLibrary("com.typesafe" % "config" % "1.3.0")
.inProject )
According to the documentation, this should place any class under
com.typesafe.config under the new package my_conf.
For your case, the solution would be adding something like this to your build.sbt file:
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("com.somecompany.**" -> "my_conf.#1")
.inLibrary("com.somecompany" % "libraryZ" % "0.11")
.inProject
)
In sbt, conflicts between libraries are configured using the conflict manager. By default, the latest revision is selected but this can also be overridden in you .sbt file:
conflictManager := ConflictManager.strict
If you're using sbt 0.13.6 or greater you will be warned when you have an incompatible binary version between your dependencies. In this situation, you could configure an override in your sbt file for the specific library:
dependencyOverrides += "org.raman" % "Z" % "x.11"
This will force the resolved version of Z to x.11 but not pull a direct dependency in.
I have written a Scala program with Eclipse Scala IDE that uses scala.util.parsing.JSON and I would like to transform it to support Scala 2.11 version. In Scala 2.11 I get an error:
error: object parsing is not a member of package util.
I have found out that the parsing library is not anymore in the util package by default, but has to be downloaded separately here.
I have downloaded that and tried to add it to my Eclipse project as a new source folder, but that didn't work out. The instructions are only for adding it to sbt, but I don't think that is relevant to me if I want to just use it in Eclipse.
Should I try to find a JAR file somewhere?
Should I try to find a JAR file somewhere?
Yes, you should. :)
And specifically, you should use this one (in SBT syntax):
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.2"
The above line should be all you need to add to build.sbt if you're using SBT. If you want to manually add it to your project by downloading it, you can find it on Maven Central.
The scala-parser-combinators library was removed in 2.11 so that people who don't use it don't have to pay a price for having it in the scala runtime library. Consequently, people who do want to use it have to now explicitly include it in their build. Note that the XML library was similarly removed in 2.11 to its own library.
is any IDE supporting SBT in a proper way (like Maven for example)? Because I've found a lot of tools that generate IDE-related configuration files but I haven't found any plugins that give any support of SBT interaction form within IDE.
I want to make an IDE-agnostic project based on SBT, but also I want to be able to use full spectrum of features that IDE provide and not just use it as an editor and do all the other stuff from console.
Does Intellij fit the bill ? It has an SBT plugin (and a Scala plugin, obviously!)
I know this isn't exactly what you're looking for, but putting it here as a work-around for working with SBT in eclipse for whoever is interested.
SBT generates eclipse config files, but after you import it, it works fine from within eclipse. You just need to set up the project for the first time outside of Eclipse, run SBT to resolve dependencies, generate eclipse structure using the eclipse sbt plugin and import into Eclipse. After that, you can run the code directly from Eclipse and it works fine.
Here're the steps in detail:
Create the folder structure as follows:
Create a file called plugins.sbt in the project folder and add the following line to it:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
Create build.sbt in the root directory i.e. ScalaSBTProject with content similar to the following. I'm using akka here, but add and remove libraries as you require:
name := "ScalaSBTProject"
version := "1.0"
scalaVersion := "2.10.0-RC2"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "com.typesafe.akka" % "akka-cluster-experimental_2.10.0-RC2" % "2.1.0-RC2"
Open command prompt and run sbt in the directory ScalaSBTProject. SBT will download and resolve whatever dependencies are required
Run the command eclipse at the SBT command line. This will generate all the eclipse related project files
Import ScalaSBTProject into Eclipse using File->Import->Existing Project to workspace, and make sure you check Import into workspace
EDIT: Just as a Post-Script, you can quite easily create a batch file to take the name of the project and generate the eclipse compatible project, just a way to speed up the process.