Adding joda-time as manageable dependency via sbt - scala

I'm having trouble using joda-time in my scala project in scala-ide. I have the following line:
import org.joda.time.DateTime
But it causes the following error: object joda is not a member of package org
That's what I did:
I put these lines in build.sbt:
libraryDependencies += "joda-time" % "joda-time" % "2.9.3"
libraryDependencies += "org.joda" % "joda-convert" % "1.8"
Then I ran reload in my sbt session.
Then I ran update in my sbt session.
So what did I miss?

sbt eclipse fixed the thing.
But it was another issue: scala-ide ceased to find main class when trying to run any module extended from App. Removing src folder and creating it again nailed that bitch down.

try to delete all joda-time jars from your computer, and reload the project again,
it seems that the jar is corrupted.

Related

How to add dedendency in SBT project

I am new scala and SBT. I am trying write a pursuing engine which can read the JSON string to object and write back Object to JSON string. To do that I am using eclipse as IDE and SBT(0.13.13) as build tool and jackson. I am getting some error while trying to import the dependency in scala file. My steps as follows:
I have added this two dependency in build.sbt of my project
libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.5.3"
libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.8.8"
Compile the project from SBT console with "compile" command
Creating a scala object for DAO object, which will contain the data and transfer as JSON
But when I am trying to import this in scala source file:
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
It's giving some compilation error:
object fasterxml is not a member of package com
But my compilation, which I did to download those dependency, ends up with success and showing download as [SUCCESSFUL]
I think I am missing something, can any one help me out in this regard?
As part of experiment I have added MySql dependency in my project as:
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.40"
Which is working properly, I am able to connect the MySql Server from scala code.
One more thing as in Maven we can update or if we save the POM file, maven downloads all those dependency automatically. Is that the same in SBT or every time after adding dependency in build.sbt I have to "reload" and "compile" to download?
Here are some of the steps you need to follow
1) clean your project in eclipse (eclipse might cache dependencies)
2) make sure your project directories are as explained in here
3) If above two steps doesn't work do
import _root_.com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import _root_.com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import _root_.com.fasterxml.jackson.module.scala.DefaultScalaModule
Hope you get it solved

Not found object error when importing external library in intellij

Here is my sbt file myproject/build.sbt
version := "1.0"
scalaVersion := "2.12.1"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % "2.4.16",
"io.circe" %% "circe-core" % "0.6.1",
"io.circe" %% "circe-generic" % "0.6.1",
"io.circe" %% "circe-parser" % "0.6.1"
)
Here is my scala file myproject/src/test.scala
package mytest
import akka._
object test {
def main(args: Array[String]) {
print(2)
}
}
I verified that my external library contains, akka
but intellij keep saying that
Error:(7, 8) not found: object akka
import akka._
I am using intellij community edition 2016.3 with the latest scala plugin (which should include latest sbt)
Can someone give me a hint on how to resolve this?
To fix the problem, you have to place your Scala source file into src/main/scala directory. Otherwise IntelliJ/SBT can't recognize it as file related to the project, so it can't associate project dependencies with it.
By default Scala source files can be placed either in the root directory of your project, or in src/main/scala (for main sources, there is also src/test/scala for tests).
If you want to use some other directories to store your Scala source files, you can configure it this way in your build.sbt:
sourceDirectories in Compile += new File("src")
I had a similar problem and it was nothing to do with the directory structure in my case. IntelliJ asks you to refresh when you add a new dependency in build.sbt. I also manually refreshed it form the SBT Shell and still same error.
In the end I closed the project and re-opened and it was fixed.

Using SORM with Play Framework 2.3.8

I am going through the Video introduction to Play Framework and, but I am stuck creating a DB object with SORM because the import fails.
I tried to add the dependencies in plugins.sbt, and relaunched activator, but it seems that activator cannot find the dependencies and I get and I get an unresolved error:
addSbtPlugin("org.sorm-framework" % "sorm" % "0.3.14")
addSbtPlugin("com.h2database" % "h2" % "1.4.181")
I got the versions from the Yvis repository. I also tried other versions with no better luck.
should be add as
libraryDependencies += "org.sorm-framework" % "sorm" % "0.3.16"
in build.sbt,
but not as
addSbtPlugin("org.sorm-framework" % "sorm" % "0.3.14")
in project/plugins.sbt
good answer by jilen; also, it can be a good idea to re-run sbt after changing the build.sbt, e.g. (in project root dir):
$ sbt
....
$ compile
...
$ eclipse
(the last two apply to Eclipse users more - and then restart Eclipse to make sure the changes are picked up (Refresh/Clean may not be enough)).

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.

How does one get sbt-idea to work in scala-2.10 project?

I had a lot of trouble getting sbt-idea to work in my Scala 2.10 project.
I tried compiling sbt-idea from its git repo, making sure that to have set
scalaVersion := "2.10.0-RC5"
in build/Build.scala, and using publish-local command to compile it in git. But I nevertheless keep getting
[error] sbt.IncompatiblePluginsException: Binary incompatibility in plugins detected.
when I then use that in my published version, say by simply adding
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.3.0-SNAPSHOT")
to the project/plugins.sbt file.
Don't think you need to build SBT for Scala 2.10. I keep my gen-idea and eclipse project generators in the global build.sbt file and it works for all my projects (or so it seems ;-)
I'm using Ubuntu, so where the SBT config files are saved on your computer may be different.
Create a folder called plugins under the hidden sbt directory. On Linux this is located at ~/.sbt (where tilde is an alias for your home directory). So now you should have ~/.sbt/plugins
Then create a file called build.sbt under this directory and add the following to it:
resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
resolvers += "Sonatype releases" at "https://oss.sonatype.org/content/repositories/releases/"
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0-SNAPSHOT")
To test, I just generated a scala 2.10 project with it, and it seems fine.
Oh, the file above also adds support for the eclipse command in SBT if you want to generate Scala-IDE projects.
I was able to use an older version of gen-idea by adding the following to project/plugins.sbt in the project itself:
import sbt._
import Defaults._
libraryDependencies += sbtPluginExtra(
m = "com.github.mpeltonen" % "sbt-idea" % "1.2.0", // Plugin module name and version
sbtV = "0.12", // SBT version
scalaV = "2.9.2" // Scala version compiled the plugin
)