Is there a Python-ealy way to run Scala scripts with dependencies?

I still know that

  • Create an ant build.xml file, complete the compilation and launch tasks, and include the corresponding banks in classpath=

  • Make an sbt project and include dependencies with version numbers in build.sbt

  • Create a maven project and include the dependencies in the XML file

  • Run -classpath explicitly from the command line

None of them are bad, but it feels like extra work after being lured with

 import json json.loads('[1, 2]') 

and having this job right off the bat if I have json installed. In particular, keeping track of relevant versions on Mavenhub is getting a little tedious.

Although maybe I'm just too picky -)

+4
source share
2 answers

What you need is xsbtscript: https://github.com/paulp/xsbtscript

It allows you to create one script file that includes both the sbt configuration required by your code and the Scala code itself.

+7
source

I think the rocks from SBT are better, Either install conscript and run this command:

 cs harrah/xsbt --branch v0.10.1 

Or create it manually:

 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)) 

The xsbtscript floor is basically a shell that downloads and installs all the necessary components to do the same. It usually works well, but has some limitations (for example, authenticated proxies will not pass).

+2
source

All Articles