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
sbtand download the Mechanize dependencies, how to you discern the necessaryimportstatements 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.
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
build.sbt
Crawler.scala
Install dependencies and run it
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:
Then, back in your Scala app’s root directory:
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-ideacommand so the IDE can pick it up.