【问题标题】:First steps in sbtsbt 的第一步
【发布时间】:2015-11-18 14:52:59
【问题描述】:

我只是想避免在scalac <some file>run <some class> 循环中缓慢启动和清理JVM。也就是说,我要求一个可以加载一次然后多次编译和运行我的应用程序的环境。在#scala 频道,我得到了使用sbt 的建议。

我曾经在#progfun 课程中使用过现成的 sbt 脚本,但我自己从未编写过 sbt。它看起来像一个地狱。您如何为我的任务轻松配置它?

【问题讨论】:

    标签: jvm sbt startup scalac


    【解决方案1】:
    % 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"
    

    如果您愿意,以后可以使用setsession save 添加更多设置,或者直接编辑文件。

    如果你没有明确设置scalaVersion,很遗憾你会得到 Scala 2.10 而不是 2.11 :-(

    【讨论】:

    • 令人惊讶的是,我们没有在任何地方指定主类。我发现run-main MyProject 可以解决问题,我认为成为Getting Started 的一员是值得的。
    • 我不得不使用set includeFilter in (Compile, unmanagedSources) := "*.scala" || "*.sc" 将工作表文件包含到编译中。
    猜你喜欢
    • 2021-11-22
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    相关资源
    最近更新 更多