Search code examples
jvmsbtstartupscalac

First steps in sbt


I just wanted to avoid slow JVM start up and cleanup in the scalac <some file> and run <some class> loop. That is, I asked for an environment that can be loaded once and then compile and run my app multiple times. At #scala channel, I got a recommendation to use sbt.

I once used ready-made sbt script during #progfun course but never programmed sbt myself. It looks like a hell. How do you easily configure it for my task?


Solution

  • % mkdir myproj 
    % cd myproj 
    % echo 'object MyProject extends App { println("hello world") }' > MyProject.scala
    % sbt
    [info] Loading global plugins from /Users/tisue/.sbt/0.13/plugins
    [info] Set current project to myproj (in build file:/Users/tisue/myproj/)
    > set scalaVersion := "2.11.7"
    [info] Defining *:scalaVersion
    [info] The new value will be used by *:Additional arguments for the presentation compiler., *:allDependencies and 13 others.
    [info]  Run `last` for details.
    [info] Reapplying settings...
    [info] Set current project to myproj (in build file:/Users/tisue/myproj/)
    > session save
    [info] Reapplying settings...
    [info] Set current project to myproj (in build file:/Users/tisue/myproj/)
    > run
    [info] Updating {file:/Users/tisue/myproj/}myproj...
    [info] Resolving jline#jline;2.12.1 ...
    [info] Done updating.
    [info] Compiling 1 Scala source to /Users/tisue/myproj/target/scala-2.11/classes...
    [info] Running MyProject 
    hello world
    [success] Total time: 2 s, completed Nov 18, 2015 9:46:26 PM
    > 
    

    if you want you can put your sources under src/main/scala instead of at the root level of the project directory — that works too.

    session save creates a build.sbt file that looks like:

    scalaVersion := "2.11.7"
    

    you can add more settings there later if you like, either with set and session save, or by editing the file directly.

    if you don't set scalaVersion explicitly, sadly you'll get Scala 2.10 instead of 2.11 :-(