sbt|sbt first try

安装 按照官网的方法,在macbook上用homebrew安装sbt

$ brew install sbt ==> Using the sandbox ==> Downloading https://github.com/sbt/sbt/releases/download/v0.13.15/sbt-0.13.1 Already downloaded: /Users/yangjia/Library/Caches/Homebrew/sbt-0.13.15.tgz ==> Caveats You can use $SBT_OPTS to pass additional JVM options to SBT: SBT_OPTS="-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M" This formula is now using the standard lightbend sbt launcher script. Project specific options should be placed in .sbtopts in the root of your project. Global settings should be placed in /usr/local/etc/sbtopts ==> Summary /usr/local/Cellar/sbt/0.13.15: 378 files, 63.3MB, built in 3 seconds

有时候从github下载总是失败。可以手动从官网下载sbt-0.13.15压缩包,解压后在/usr/local/bin下建了sbt的软链接。
$ ln -s sbt /your/sbt

?第一个demo工程 继续照着官网的教程,建第一个sbt工程:
$ sbt new sbt/scala-seed.g8 WARN: No sbt.version set in project/build.properties, base directory: /Users/yangjia/sources/learning_scala/sbt [warn] Executing in batch mode. [warn] For better performance, hit [ENTER] to switch to interactive mode, or [warn] consider launching sbt without any commands, or explicitly passing 'shell' [info] Set current project to sbt (in build file:/Users/yangjia/sources/learning_scala/sbt/) [info] Resolving org.scala-sbt.sbt-giter8-resolver#sbt-giter8-resolver_2.10; 0.1.[info] Resolving org.scala-sbt.sbt-giter8-resolver#sbt-giter8-resolver_2.10; 0.1.[info] Resolving org.eclipse.jgit#org.eclipse.jgit-parent; 3.7.0.201502260915-r .[info] Resolving org.eclipse.jgit#org.eclipse.jgit-parent; 3.7.0.201502260915-r .[info] Resolving org.eclipse.jgit#org.eclipse.jgit.archive; 3.7.0.201502260915-r [info] Resolving org.eclipse.jgit#org.eclipse.jgit.archive; 3.7.0.201502260915-r [info] Resolving org.eclipse.jgit#org.eclipse.jgit-parent; 3.7.0.201502260915-r .[info] Resolving org.eclipse.jgit#org.eclipse.jgit-parent; 3.7.0.201502260915-r .[info] Resolving org.eclipse.jgit#org.eclipse.jgit-parent; 3.7.0.201502260915-r .[info] Resolving org.codehaus.plexus#plexus-container-default; 1.0-alpha-9-stable[info] Resolving org.codehaus.plexus#plexus-container-default; 1.0-alpha-9-stable[info] Resolving org.slf4j#slf4j-parent; 1.7.20 ... [info] downloading https://repo1.maven.org/maven2/org/scala-sbt/sbt-giter8-resolver/sbt-giter8-resolver_2.10/0.1.3/sbt-giter8-resolver_2.10-0.1.3.jar ... [info] [SUCCESSFUL ] org.scala-sbt.sbt-giter8-resolver#sbt-giter8-resolver_2.10; 0.1.3!sbt-giter8-resolver_2.10.jar (744ms) [info] downloading https://repo1.maven.org/maven2/org/foundweekends/giter8/giter8_2.10/0.7.2/giter8_2.10-0.7.2.jar ... [info] [SUCCESSFUL ] org.foundweekends.giter8#giter8_2.10; 0.7.2!giter8_2.10.jar (825ms) [info] downloading https://repo1.maven.org/maven2/org/foundweekends/giter8/giter8-lib_2.10/0.7.2/giter8-lib_2.10-0.7.2.jar ... [info] [SUCCESSFUL ] org.foundweekends.giter8#giter8-lib_2.10; 0.7.2!giter8-lib_2.10.jar (1853ms) [info] downloading https://repo1.maven.org/maven2/com/github/scopt/scopt_2.10/3.5.0/scopt_2.10-3.5.0.jar ... [info] [SUCCESSFUL ] com.github.scopt#scopt_2.10; 3.5.0!scopt_2.10.jar (1397ms) [info] downloading https://repo1.maven.org/maven2/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar ... [info] [SUCCESSFUL ] ch.qos.logback#logback-classic; 1.1.7!logback-classic.jar (1563ms) [info] downloading https://repo1.maven.org/maven2/org/clapper/scalasti_2.10/2.1.2/scalasti_2.10-2.1.2.jar ... [info] [SUCCESSFUL ] org.clapper#scalasti_2.10; 2.1.2!scalasti_2.10.jar (801ms) [info] downloading https://repo1.maven.org/maven2/org/eclipse/jgit/org.eclipse.jgit.pgm/3.7.0.201502260915-r/org.eclipse.jgit.pgm-3.7.0.201502260915-r.jar ... [info] [SUCCESSFUL ] org.eclipse.jgit#org.eclipse.jgit.pgm; 3.7.0.201502260915-r!org.eclipse.jgit.pgm.jar (1122ms) [info] downloading https://repo1.maven.org/maven2/commons-io/commons-io/2.4/commons-io-2.4.jar... [info] [SUCCESSFUL ] commons-io#commons-io; 2.4!commons-io.jar (1024ms) [info] downloading https://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/2.7.1/plexus-archiver-2.7.1.jar ... [info] [SUCCESSFUL ] org.codehaus.plexus#plexus-archiver; 2.7.1!plexus-archiver.jar (926ms) [info] downloading https://repo1.maven.org/maven2/org/clapper/grizzled-scala_2.10/3.1.0/grizzled-scala_2.10-3.1.0.jar ... [info] [SUCCESSFUL ] org.clapper#grizzled-scala_2.10; 3.1.0!grizzled-scala_2.10.jar (1480ms) [info] downloading https://repo1.maven.org/maven2/org/clapper/classutil_2.10/1.1.0/classutil_2.10-1.1.0.jar ... [info] [SUCCESSFUL ] org.clapper#classutil_2.10; 1.1.0!classutil_2.10.jar (834ms) [info] downloading https://repo1.maven.org/maven2/org/antlr/ST4/4.0.8/ST4-4.0.8.jar ... [info] [SUCCESSFUL ] org.antlr#ST4; 4.0.8!ST4.jar (953ms) [info] downloading https://repo1.maven.org/maven2/org/ow2/asm/asm/5.1/asm-5.1.jar ... [info] [SUCCESSFUL ] org.ow2.asm#asm; 5.1!asm.jar (770ms) [info] downloading https://repo1.maven.org/maven2/org/ow2/asm/asm-commons/5.1/asm-commons-5.1.jar ... [info] [SUCCESSFUL ] org.ow2.asm#asm-commons; 5.1!asm-commons.jar (767ms) [info] downloading https://repo1.maven.org/maven2/org/ow2/asm/asm-util/5.1/asm-util-5.1.jar ... [info] [SUCCESSFUL ] org.ow2.asm#asm-util; 5.1!asm-util.jar (764ms) [info] downloading https://repo1.maven.org/maven2/org/ow2/asm/asm-tree/5.1/asm-tree-5.1.jar ... [info] [SUCCESSFUL ] org.ow2.asm#asm-tree; 5.1!asm-tree.jar (771ms) [info] downloading https://repo1.maven.org/maven2/org/antlr/antlr-runtime/3.5.2/antlr-runtime-3.5.2.jar ... [info] [SUCCESSFUL ] org.antlr#antlr-runtime; 3.5.2!antlr-runtime.jar (857ms) [info] downloading https://repo1.maven.org/maven2/args4j/args4j/2.0.12/args4j-2.0.12.jar ... [info] [SUCCESSFUL ] args4j#args4j; 2.0.12!args4j.jar (789ms) [info] downloading https://repo1.maven.org/maven2/org/apache/commons/commons-compress/1.6/commons-compress-1.6.jar ... [info] [SUCCESSFUL ] org.apache.commons#commons-compress; 1.6!commons-compress.jar (985ms) [info] downloading https://repo1.maven.org/maven2/org/eclipse/jgit/org.eclipse.jgit.archive/3.7.0.201502260915-r/org.eclipse.jgit.archive-3.7.0.201502260915-r.jar ... [info] [SUCCESSFUL ] org.eclipse.jgit#org.eclipse.jgit.archive; 3.7.0.201502260915-r!org.eclipse.jgit.archive.jar (747ms) [info] downloading https://repo1.maven.org/maven2/org/eclipse/jgit/org.eclipse.jgit/3.7.0.201502260915-r/org.eclipse.jgit-3.7.0.201502260915-r.jar ... [info] [SUCCESSFUL ] org.eclipse.jgit#org.eclipse.jgit; 3.7.0.201502260915-r!org.eclipse.jgit.jar (1947ms) [info] downloading https://repo1.maven.org/maven2/org/eclipse/jgit/org.eclipse.jgit.ui/3.7.0.201502260915-r/org.eclipse.jgit.ui-3.7.0.201502260915-r.jar ... [info] [SUCCESSFUL ] org.eclipse.jgit#org.eclipse.jgit.ui; 3.7.0.201502260915-r!org.eclipse.jgit.ui.jar (743ms) [info] downloading https://repo1.maven.org/maven2/org/tukaani/xz/1.4/xz-1.4.jar ... [info] [SUCCESSFUL ] org.tukaani#xz; 1.4!xz.jar (997ms) [info] downloading https://repo1.maven.org/maven2/org/osgi/org.osgi.core/4.3.1/org.osgi.core-4.3.1.jar ... [info] [SUCCESSFUL ] org.osgi#org.osgi.core; 4.3.1!org.osgi.core.jar (878ms) [info] downloading https://repo1.maven.org/maven2/com/googlecode/javaewah/JavaEWAH/0.7.9/JavaEWAH-0.7.9.jar ... [info] [SUCCESSFUL ] com.googlecode.javaewah#JavaEWAH; 0.7.9!JavaEWAH.jar(bundle) (782ms) [info] downloading https://repo1.maven.org/maven2/org/apache/httpcomponents/httpclient/4.1.3/httpclient-4.1.3.jar ... [info] [SUCCESSFUL ] org.apache.httpcomponents#httpclient; 4.1.3!httpclient.jar (875ms) [info] downloading https://repo1.maven.org/maven2/org/apache/httpcomponents/httpcore/4.1.4/httpcore-4.1.4.jar ... [info] [SUCCESSFUL ] org.apache.httpcomponents#httpcore; 4.1.4!httpcore.jar (803ms) [info] downloading https://repo1.maven.org/maven2/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar ... [info] [SUCCESSFUL ] commons-logging#commons-logging; 1.1.1!commons-logging.jar (755ms) [info] downloading https://repo1.maven.org/maven2/commons-codec/commons-codec/1.4/commons-codec-1.4.jar ... [info] [SUCCESSFUL ] commons-codec#commons-codec; 1.4!commons-codec.jar (754ms) [info] downloading https://repo1.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9-stable-1/plexus-container-default-1.0-alpha-9-stable-1.jar ... [info] [SUCCESSFUL ] org.codehaus.plexus#plexus-container-default; 1.0-alpha-9-stable-1!plexus-container-default.jar (809ms) [info] downloading https://repo1.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.18/plexus-utils-3.0.18.jar ... [info] [SUCCESSFUL ] org.codehaus.plexus#plexus-utils; 3.0.18!plexus-utils.jar (828ms) [info] downloading https://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/2.2/plexus-io-2.2.jar ... [info] [SUCCESSFUL ] org.codehaus.plexus#plexus-io; 2.2!plexus-io.jar (763ms) [info] downloading https://repo1.maven.org/maven2/ch/qos/logback/logback-core/1.1.7/logback-core-1.1.7.jar ... [info] [SUCCESSFUL ] ch.qos.logback#logback-core; 1.1.7!logback-core.jar (945ms) [info] downloading https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.20/slf4j-api-1.7.20.jar ... [info] [SUCCESSFUL ] org.slf4j#slf4j-api; 1.7.20!slf4j-api.jar (749ms) Minimum Scala build. name [My Something Project]: helloworld Template applied in ./helloworld

这里用到了sbt 0.13.13新增的new命令,这个命令通过?模板创建一个构建工程(build definition)。正如命令输出,new命令实际上是用Giter8。Giter8基于模板系统,我们这次用到的scala/scala-seed.g8只是他众多模板的其中一个。scala-seed是scala的官方?模板。
在下载所有的依赖库后,sbt提示输入工程名,我填的是helloworld
最后会生成如下的目录结构

sbt|sbt first try
文章图片
图片发自App
运行工程
  1. helloworld目录下,输入sbt进入sbt shell。需要注意的是,new生成的?配置文件build.properities指定的sbt?版本是0.13.13。进入shell后,会自动下载0.13.13版本的sbt,如下所示:
banxia:helloworld yangjia$ sbt Getting org.scala-sbt sbt 0.13.13 (this may take some time)...

可以手动把build.properitiessbt?版本改为0.13.15,再进shell就不再下载老版本了。
$ sbt [info] Loading project definition from /Users/yangjia/sources/learning_scala/sbt/helloworld/project [info] Updating {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/project/}helloworld-build... [info] Resolving org.scala-sbt.ivy#ivy; 2.3.0-sbt-48dd0744422128446aee9ac31aa356e[info] Resolving org.fusesource.jansi#jansi; 1.4 ... [info] Done updating. [info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/project/target/scala-2.10/sbt-0.13/classes... [info] 'compiler-interface' not yet compiled for Scala 2.10.6. Compiling... [info] Compilation completed in 17.091 s [info] Set current project to Hello (in build file:/Users/yangjia/sources/learning_scala/sbt/helloworld/) >

【sbt|sbt first try】shell?准备OK后,打印一个>,等待输入。
  1. 输入run,执行Hello.scala
> run [info] Updating {file:/Users/yangjia/sources/learning_scala/sbt/helloworld/}root... [info] Resolving org.sonatype.oss#oss-parent; 9 ... [info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-library/2.12.1/scala-library-2.12.1.jar ... [info] [SUCCESSFUL ] org.scala-lang#scala-library; 2.12.1!scala-library.jar (3126ms) [info] downloading https://repo1.maven.org/maven2/org/scalatest/scalatest_2.12/3.0.1/scalatest_2.12-3.0.1.jar ... [info] [SUCCESSFUL ] org.scalatest#scalatest_2.12; 3.0.1!scalatest_2.12.jar(bundle) (2438ms) [info] downloading https://repo1.maven.org/maven2/org/scalactic/scalactic_2.12/3.0.1/scalactic_2.12-3.0.1.jar ... [info] [SUCCESSFUL ] org.scalactic#scalactic_2.12; 3.0.1!scalactic_2.12.jar(bundle) (1806ms) [info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-reflect/2.12.1/scala-reflect-2.12.1.jar ... [info] [SUCCESSFUL ] org.scala-lang#scala-reflect; 2.12.1!scala-reflect.jar (1551ms) [info] downloading https://repo1.maven.org/maven2/org/scala-lang/modules/scala-xml_2.12/1.0.5/scala-xml_2.12-1.0.5.jar ... [info] [SUCCESSFUL ] org.scala-lang.modules#scala-xml_2.12; 1.0.5!scala-xml_2.12.jar(bundle) (1212ms) [info] downloading https://repo1.maven.org/maven2/org/scala-lang/modules/scala-parser-combinators_2.12/1.0.4/scala-parser-combinators_2.12-1.0.4.jar ... [info] [SUCCESSFUL ] org.scala-lang.modules#scala-parser-combinators_2.12; 1.0.4!scala-parser-combinators_2.12.jar(bundle) (3376ms) [info] downloading https://repo1.maven.org/maven2/org/scala-lang/scala-compiler/2.12.1/scala-compiler-2.12.1.jar ... [info] [SUCCESSFUL ] org.scala-lang#scala-compiler; 2.12.1!scala-compiler.jar (3747ms) [info] downloading https://repo1.maven.org/maven2/org/scala-lang/modules/scala-xml_2.12/1.0.6/scala-xml_2.12-1.0.6.jar ... [info] [SUCCESSFUL ] org.scala-lang.modules#scala-xml_2.12; 1.0.6!scala-xml_2.12.jar(bundle) (1782ms) [info] downloading https://repo1.maven.org/maven2/jline/jline/2.14.1/jline-2.14.1.jar ... [info] [SUCCESSFUL ] jline#jline; 2.14.1!jline.jar (1977ms) [info] Done updating. [info] Compiling 1 Scala source to /Users/yangjia/sources/learning_scala/sbt/helloworld/target/scala-2.12/classes... [info] 'compiler-interface' not yet compiled for Scala 2.12.1. Compiling... [info] Compilation completed in 17.956 s [info] Running example.Hello hello [success] Total time: 67 s, completed 2017-7-15 16:40:24

    推荐阅读