The ways I know about so far are
Create an ant build.xml
file, make compile and run tasks, and include appropriate jars in a classpath=
Make at sbt
project and include dependencies with version numbers in build.sbt
Make a maven project and include dependencies in the xml file
Run from the command line setting -classpath
explicitly
None of these are bad, but it feels like extra work after being babied with
import json
json.loads('[1, 2]')
and having that work right off the bat, provided I have json
installed. In particular tracking down appropriate versions on Mavenhub gets a little tiresome.
Though maybe I'm just being too picky ;-)
What you want is xsbtscript: https://github.com/paulp/xsbtscript
It allows you to create a single script file which includes both the sbt config your code requires along with the Scala code itself.
I think scalas from SBT is better. Either install conscript and run this command:
cs harrah/xsbt --branch v0.10.1
Or create it by hand:
java -Dsbt.main.class=sbt.ScriptMain -Dsbt.boot.directory=/home/user/.sbt/boot -jar sbt-launch.jar "$@"
And then use it like this:
#!/usr/bin/env scalas
!#
/***
scalaVersion := "2.9.0-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))
Paul's xsbtscript is basically a shell that downloads and install all necessary components to do the same thing. It usually works well, but has some limitations (won't go through authenticated proxies, for instance).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With