Using IntelliJ, how to add dependency in an sbt project - scala

I'm new to sbt and I wanted to learn it with a small Scala project in IntelliJ.
I started with the official sbt Getting Started guide, to learn the sbt basics on the console (https://www.scala-sbt.org/1.x/docs/sbt-by-example.html). Following the guide, everything compiles fine.
Then I created an sbt project in IntelliJ, trying to do the same thing there. When I add the org.scalatest depenpency to the build.sbt file the project can no longer compile. The error message is:
module not found: org.scalatest#scalatest_2.13;3.0.5
When I created the fresh sbt project in IntelliJ, first the build.sbt looked something like this:
name := "sbtTest"
version := "1.0"
scalaVersion := "2.13.0"
Then I added the dependency:
name := "sbtTest"
version := "1.0"
scalaVersion := "2.13.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
After reloading the build file and getting the error when compiling, I tried to change the biuld.sbt according to the code which already worked in the sbt Getting Started guide:
ThisBuild / scalaVersion := "2.13.0"
ThisBuild / organization := "me"
ThisBuild / version := "1.0"
lazy val sbtTest = (project in file("."))
.settings(
name := "sbtTest",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
)
This has again the same error after reloading and compiling.
My sbt version is 1.2.8.
Is something wrong with my build.sbt? Or is the sbt version too new for IntelliJ? Is IntelliJ probably not the recommended IDE to create Sala sbt projects?

Structure of both your build.sbt is ok. The thing seems to be in versions.
Scalatest 3.0.5 is currently unavailable for Scala 2.13.0. It's available for Scala 2.13.0-M2.
It's Scalatest 3.0.8 that is available for Scala 2.13.0.
https://mvnrepository.com/artifact/org.scalatest/scalatest
After you fix versions in build.sbt re-import IntelliJ project in pop-up window
or in sbt tool window
or in sbt shell

Related

Scala libraries incompatibility

akka-actor_2.13-2.5.23.jar of play-scala-seed build path is cross-compiled with an incompatible version of Scala (2.13.0). In case this report is mistaken, this check can be disabled in the compiler preference page.
I have many errors of this type in my scala project. How do I resolve such errors?
I am using Scala IDE.
build.sbt:
name := """play-scala-seed"""
organization := "com.example"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.0"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "4.0.3" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.example.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.example.binders._"
I guess I need to change Scala installation to 2.13, right? ( I am referring to the Scala installation option that shows up in the project properties after right clicking on the project in the IDE )
But, the only options available in the dropdown are 2.10,2.11 and 2.12. How do I add 2.13 into my IDE?
The solutions in similar questions did not work for me.

Spark-Scala build.sbt libraryDependencies UnresolvedDependency

i'm trying to import a dependency in my build.sbt file from here
https://github.com/dmarcous/spark-betweenness.
When i hover on the error it says:
Expression type ModuleID must confirm to Def.SettingsDefinition in SBT file
Unresolved Dependency
I am new in scala so my question may be silly.Thanks in advance
It is still unclear how your build configuration looks like, but the following build.sbt works (in the sense that it compiles and does not show the error that you mentioned):
name := "test-sbt"
organization := "whatever"
version := "1.0.0"
scalaVersion := "2.10.7"
libraryDependencies += "com.centrality" %% "spark-betweenness" % "1.0.0"
Alternatively, if you have a multi-project build, it could look like this:
lazy val root = project
.settings(
name := "test-sbt",
organization := "whatever",
version := "1.0.0",
scalaVersion := "2.10.7",
libraryDependencies += "com.centrality" %% "spark-betweenness" % "1.0.0"
)
However, you're probably going to find that it still does not work because it cannot resolve this dependency. Indeed, this library does not seem to be available neither in Maven Central nor in jcenter. It is also very old - it appears to only be published for Scala 2.10 and a very old Spark version (1.5), so most likely you won't be able to use it with recent Spark environments (2.x and Scala 2.11).

Downgrade Scala from 2.12 to 2.11.x

I am learning Scala and downloaded IntelliJ Idea. I installed the Scala plugin and was instantly given the 2.12 version. Now I am trying to downgrade to 2.11 because I need this version to follow along a Coursera class I am taking.
I am having the same "UNRESOLVED DEPENDENCIES" shown in the link below:
SBT project refresh failed [IntelliJ, Scala, SBT]
I tried to solve my problem by doing what #Haspemulator suggested, but I'm still getting error messages. Here is a screenshot of what I have now:
(Notice that there is a folder called scala-2.12)
The error you get states that "Cannot add dependency ... to configuration "Test" because this configuration doesn't exist!" There's no such predefined configuration as "Test". There's only "test" (lower case). Try to use it instead.
CatherineAlv has already solved the problem. I am just segregating the steps together.
I too faced this problem while setting up scala for coursera course "Functional Programming Principles in Scala". By default build.sbt was showing scalaVersion as 2.12.x but I needed 2.11.x for the course. This can be easily solved in two steps:
Change the scalaVersion in build.sbt. The code would look like after change:
name := "Example"
version := "1.0"
scalaVersion := "2.11.8"
Build it.
Add the libraryDependencies to build.sbt. The code would now look like this:
name := "Example"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.6" % "test"
Build again.
This should solve it.

Creating empty eclipse project using sbt

When I run eclipse in sbt with build.sbt having,
name := "test"
version := "0.1"
scalaVersion := "2.10.5"
It had build.sbt, project and target but, in tutorial I was following it was mentioned src/main/scala, src/main/java, ..test will be available where I can code in eclipse by importing.
What am I doing wrong?
I have added addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0") in ~\.sbt\0.13
Appreciate some help

How to run playframework sample in Intelllij 13?

I am using Intellij 13 Ultimate and want to create a Play Framework sample. But I am unable to build this project because it always throws this error:
object index is not a member of package views.html
Ok(views.html.index("Your new application is ready."))
I have tried this on both Mac and Windows platforms, but the error is always the same.
How can I solve this?
The generation works just fine and all the paths are correctly added to the build. However the routes are not (yet) compiled by the plugins (scala+playframework), thus the reverse routing classes generated by play are not available for intellij.
You will have the same problem with the templates.
If you run a play compile (or a sbt compile), the classes will be generated and intellij should be able to pick them up and compile your project.
I found a trick here.
If you generate the play-scala project using activator command line activator [new my_first_project play-scala]. You'll get the following sbt build file.
name := """my_first_project"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
But IF you create a project from intellj using NewProject->Scala-Play 2.x, you will get the following sbt.
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
after combine them. Ignore the name. And I set for in run to false
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
After doing that. Open the activator-generated project with Intellj, configure a run with Play 2.x run. Everything goes well.
By the way, if you open the activator-generated project before you change the sbt file. You might need to rm -r .idea
Hope that helps.
Running "play idea" in the new project root fixed it for me. The project should compile and run after reloading.
I only have this problem with projects created through Idea's New Project menu.
I have the same problem: in Idea 13 there are highlight errors there, but in Idea 12 everything is ok. The reason is that there is no Scala plugin for Idea 13, so for now it's impossible to get it to work in Idea 13.
The solution is to install Idea 12, go to Preferences -> Plugins, type "Scala" in find box, and install Scala plugin.