Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get automatic dependency resolution in my scala scripts?

Tags:

scala

I'm just learning scala coming out of the groovy/java world. My first script requires a 3rd party library TagSoup for XML/HTML parsing, and I'm loath to have to add it the old school way: that is, downloading TagSoup from its developer website, and then adding it to the class path.

Is there a way to resolve third party libraries in my scala scripts? I'm thinking Ivy, I'm thinking Grape.

Ideas?


The answer that worked best for me was to install n8:

curl https://raw.github.com/n8han/conscript/master/setup.sh | sh
cs harrah/xsbt --branch v0.11.0

Then I could import tagsoup fairly easily example.scala

  /***
      libraryDependencies ++= Seq(
          "org.ccil.cowan.tagsoup" % "tagsoup" % "1.2.1"
      )
  */

  def getLocation(address:String) = {
      ...
  }

And run using scalas:

  scalas example.scala

Thanks for the help!

like image 466
dsummersl Avatar asked Sep 29 '11 16:09

dsummersl


2 Answers

While the answer is SBT, it could have been more helpful where scripts are regarded. See, SBT has a special thing for scripts, as described here. Once you get scalas installed, either by installing conscript and then running cs harrah/xsbt --branch v0.11.0, or simply by writing it yourself more or less like this:

#!/bin/sh
java -Dsbt.main.class=sbt.ScriptMain \
     -Dsbt.boot.directory=/home/user/.sbt/boot \
     -jar sbt-launch.jar "$@"

Then you can write your script like this:

#!/usr/bin/env scalas
!#

/***
scalaVersion := "2.9.1"

libraryDependencies ++= Seq(
  "net.databinder" %% "dispatch-twitter" % "0.8.3",
  "net.databinder" %% "dispatch-http" % "0.8.3"
)
*/

import dispatch.{ json, Http, Request }
import dispatch.twitter.Search
import json.{ Js, JsObject }

def process(param: JsObject) = {
  val Search.text(txt)        = param
  val Search.from_user(usr)   = param
  val Search.created_at(time) = param

  "(" + time + ")" + usr + ": " + txt
}

Http.x((Search("#scala") lang "en") ~> (_ map process foreach println))

You may also be interested in paulp's xsbtscript, which creates an xsbtscript shell that has the same thing as scalas (I guess the latter was based on the former), with the advantage that, without either conscript or sbt installed, you can get it ready with this:

curl https://raw.github.com/paulp/xsbtscript/master/setup.sh | sh

Note that it installs sbt and conscript.

And there's also paulp's sbt-extras, which is an alternative "sbt" command line, with more options. Note that it's still sbt, just the shell script that starts it is more intelligent.

like image 165
Daniel C. Sobral Avatar answered Sep 20 '22 11:09

Daniel C. Sobral


SBT (Simple Build Tool) seems to be the build tool of choice in the Scala world. It supports a number of different dependency resolution mechanisms: https://github.com/harrah/xsbt/wiki/Library-Management

like image 38
Chris Shain Avatar answered Sep 23 '22 11:09

Chris Shain