How to add a scala library to eclipse - scala

I am trying to add this Scala Library into Eclipse so as to add available functions I can use on Actors.
I've downloaded the file and extracted it and tried adding it to my workspace in the project explorer, but when I try, Eclipse tells me it can't find any projects in the file. I'm sure that there is a tutorial or something online that explains exactly how to do this, but like I said, I'm not sure about all the terminology, so I don't know what to search for to get the result I want.

The easiest way is to create an sbt project and use the sbteclipse plugin.
Your project structure should look like this:
build.sbt
project/build.properties
project/eclipse.sbt
build.sbt
(note that lines of code should be separated by a white line)
name := "ProjectName"
libraryDependencies += "org.scala-lang.modules" %% "scala-async" % "0.9.2"
build.properties
sbt.version=0.13.5
eclipse.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")
Then from the root directory execute sbt and within the sbt command line execute eclipse or eclipse with-sources=true
You can use sbt-extras or activator to get a specialized version that will automatically download the correct sbt version based on the build.properties file.
You could add the sbteclipse plugin as a default plugin, making it available in all projects by creating eclipse.sbt in the ~/.sbt/0.13/plugins directory instead of the project directory

If you created your project via sbt, please follow the instruction on the github page of async. Namely, add this libraryDependencies += "org.scala-lang.modules" %% "scala-async" % "0.9.2" to build.sbt, run sbt, regenerate eclipse files with "eclipse" command and finally re-import project into eclipse

Related

How to declare a sbt plugin?

I have cloned sbteclipse.I am reading scala-sbt tutorial,says:
If your project is in directory hello, and you’re adding sbt-site plugin to the build definition, create hello/project/site.sbt
I am in /home/mil directory,and sbteclipse is in the same directory.Inside sbteclipse is project directory with plugins.sbt
libraryDependencies += "org.scala-sbt" % "scripted-plugin" % sbtVersion.value
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.3")
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.6.0")
Should I edit this file or not?How to declare plugin in oreder to enable eclipse to recognize them?
As a general rule, SBT plugins has to be defined in the project subfolder of your project's root. SBT aggregates definitions from all files with .sbt extension, so it doesn't really matter if you call it plugins.sbt or site.sbt. It's also fine to have multiple .sbt files.
So in your particular case, either add that plugin to the existing project/plugins.sbt, or follow the tutorial and create a new file, project/site.sbt, and add the plugin there.
P.S. this rule of searching and aggregating definitions from all .sbt files applies not only to meta-build (project directory), but also for the build itself. It might make sense to split big build definitions into multiple .sbt files. E.g. we have build.sbt for main definitions (library dependencies, etc.) and docker.sbt for Docker definitions.

Where to place downloaded ScalaTest jar so sbt uses it across projects?

I've downloaded the ScalaTest jar and have used it as in the example, but now I would like to start using it with sbt. Where do I place the downloaded jar so I can use it with sbt across multiple projects?
You don't download dependencies like ScalaTest manually. The point of using sbt is to declare your project's dependencies and let sbt download them for you automatically.
Add this line in your build.sbt file:
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.0" % "test"
For more details see official doc on setting this up.

IntelliJ 13 with SBT plugin does not recognize Scalding dependency

I am trying to add Scalding 2.10 as a managed dependency via build.sbt like so:
name := "ss"
version := "1.0"
libraryDependencies += "com.twitter" % "scalding_2.10" % "0.10.0"
IntelliJ downloads the jar and adds it as an external library (see screen below) but fails to resolve the com.twitter namespace.
I have tried both invalidating the IntelliJ cache and generating project files via sbt gen-idea but neither solutions have worked. Any ideas would be greatly appreciated.
The scalding jar file scalding_2.10 has no code in it to compile against. Its just 300 Bytes in size.
The correct dependency I feel should be
libraryDependencies += "com.twitter" % "scalding-core_2.10" % "0.11.1"
As the comment suggest try rm-ing your ivy2 cache, and try sbt gen-idea. If that doesn't work, other things to check:
makes sure you have indeed got the scala plugin installed.
Most likely you're java SDK is not set or pointing somewhere wrong; right click the project dir, click "Open Module Settings", go to SDK and make sure the path is correctly set to the jdk otherwise syntax highlighting will likely break.
To test your deps have been properly pulled in from tinternet, try sbt compile; if it compiles then you should indeed have downloaded the dependency properly.

How can IntelliJ find remote dependencies?

I added the Mailer plugin to my Play Framework 2 project. It compiles and works fine, but IntelliJ can't resolve any of its classes. I would normally just add the jar as a module in my IntelliJ project settings, but I don't have a jar. As far as I understand, the plugin is automatically being pulled from some repository. So how do I make IntelliJ aware of it?
I added this to conf/play.plugins
1500:com.typesafe.plugin.CommonsMailerPlugin
And this as a dependency in project/build.scala
"com.typesafe" %% "play-plugins-mailer" % "2.0.4"
I resolve problems like this with the sbt-idea SBT plugin. Just add this to your project/plugins.sbt file:
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0")
Then, whenever you change your project dependencies, run sbt idea and your IntelliJ project will be updated.
I had to delete the reference to com.github.mpeltonen in my plugins.sbt file first. Then as Bill said, I needed to run play idea. If I tried to run play idea without deleting the reference I got this exception:
java.lang.NoSuchMethodError: org.sbtidea.SbtIdeaPlugin$.ideaSettings()Lscala/collection/Seq;

What's the Scala + sbt workflow equivalent of Ruby + Bundler with a Gemfile?

I'm having a good time learning Scala, but I'm having the hardest time grasping how to set up a development environment.
In Ruby
File hierarchy
my_app/
|
+-- Gemfile
+-- app.rb
Gemfile
source :rubygems
gem "mechanize"
app.rb
require "mechanize"
agent = Mechanize.new
page = agent.get("http://google.com")
Install dependencies and run it
$ bundle install
$ ruby app.rb
What's the Scala equivalent with sbt?
I'm reading about sbt and how packages/imports/jar dependencies work in Java/Scala, but I can't seem to filter out the bare bones necessities.
What's the minimal file hierarchy to replicate the above with Scala?
Here's the Java Mechanize lib available on Maven: http://search.maven.org/#search|ga|1|mechanize
Once you run sbt and download the Mechanize dependencies, how to you discern the necessary import statements you need to get this to work?
val agent = new MechanizeAgent
val page: HtmlDocument = agent.get("http://www.google.com")
I got the above working in Eclipse by manually importing the .jars and then importing packages from the libraries until the compiler/runtime errors stopped and the agent worked. But that experience was discouraging and I've come here to repent.
Intent of this question: The Java ecosystem/workflow is overwhelming to me as someone that's used to Ruby's effortless, IDEless workflow. I think a bare bones equivalent would give me a place to start building upon.
Ideally, I'd like to get Scala development working with just Vim and the command line before becoming dependent on Eclipse.
sbt uses a library called ivy to import projects from the main maven repository. There are a few repositories sbt is preconfigured to work with, including the main maven repository.
Once these libraries are "resolved" (downloaded to your computer and hooked up to your project), the eclipse plugin will create dependencies to each jar in the eclipse project generated.
Here is how you configure it.
sbt Managed Dependencies
http://www.scala-sbt.org/release/docs/Getting-Started/Basic-Def.html#adding-library-dependencies
Add a dependency in your project's build.sbt file. If you add a dependency that depends on a specific version of scala, use two %% between the group and artifact name. Don't forget to add an empty line between each command in your build.sbt file.
libraryDependencies += "com.gistlabs" % "mechanize" % "0.11.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "1.6.1" % "test"
Update the dependencies by running the update command:
$ sbt update
sbt Eclipse Plugin
https://github.com/typesafehub/sbteclipse/wiki/Installing-sbteclipse
You can install the sbt eclipse plugin globally by creating a file at ~/.sbt/plugins/plugins.sbt and putting this line into it:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
Whenever you add or update a dependency, run the following command and refresh your eclipse project:
$ sbt eclipse
I'd like to go a step farther than ffk's answer, do much more hand-holding, and actually provide the direct translation of the Ruby example to Scala + sbt.
File hierarchy
Crawler/
+- build.sbt
+- src/
+- main/
+- scala/
+- Crawler.scala
build.sbt
libraryDependencies += "com.gistlabs" % "mechanize" % "0.11.0"
Crawler.scala
import com.gistlabs.mechanize.MechanizeAgent
import com.gistlabs.mechanize.document.Document
object Crawler extends App {
val agent = new MechanizeAgent
val page: Document = agent.get("http://google.com")
}
Install dependencies and run it
$ sbt run
To make the project importable into Eclipse or IntelliJ, you need the sbteclipse-plugin or sbt-idea plugin. But rather than having to declare these plugins in every build.sbt for every new project, you can declare them in a global build.sbt:
// in ~/.sbt/plugins/build.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0")
Then, back in your Scala app's root directory:
$ sbt eclipse
or
$ gen-idea
Afterwards, you should be able to open it in the respective IDE.
Note: Whenever you add dependencies in your build.sbt, you'll need to rerun the sbt eclipse/gen-idea command so the IDE can pick it up.