我只是想避免scalac <some file>
和run <some class>
循环中JVM启动和清理缓慢。也就是说,我要求一个可以加载一次,然后编译和运行我的应用程序多次的环境。在#scala频道,我得到了使用sbt
的建议。
我曾经在#progfun课程中使用过现成的sbt脚本,但从未自己编程过sbt。它看起来像地狱。如何为我的任务轻松配置它?
% 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
>
如果您愿意,您可以将源代码放在src/main/scala
下,而不是放在项目目录的根级别,这也很有效。
session save
创建一个build.sbt
文件,看起来像:
scalaVersion := "2.11.7"
如果您愿意,可以稍后在那里添加更多设置,可以使用set
和session save
,也可以直接编辑文件。
如果不显式设置scalaVersion
,那么遗憾的是,您将得到Scala 2.10而不是2.11:-(