sbt一个工程build工具的深入学习

Owen Jia 2019年04月10日 706次浏览

The interactive build tool Define your tasks in Scala. Run them in parallel from sbt's interactive shell.

构建于sbt-1.2.8版本。
这篇文章的目标是让你了解sbt并会入门基本使用它。

sbt官网

sbt使用文档

sbt包api文档

sbt代码库

sbt特点

  1. Little or no configuration required for simple projects
  2. Scala-based build definition that can use the full flexibility of Scala code
  3. Accurate incremental recompilation using information extracted from the compiler
  4. Continuous compilation and testing with triggered execution
  5. Packages and publishes jars
  6. Generates documentation with scaladoc
  7. Supports mixed Scala/Java projects
  8. Supports testing with ScalaCheck, specs, and ScalaTest. JUnit is supported by a plugin.
  9. Starts the Scala REPL with project classes and dependencies on the classpath
  10. Modularization supported with sub-projects
  11. External project support (list a git repository as a dependency!)
  12. Parallel task execution, including parallel test execution
  13. Library management support: inline declarations, external Ivy or Maven configuration files, or manual management

从网站介绍上初步了解,sbt最初的目标应该是服务于scala项目,后期发展逐步支持java,通过sbt-cpp插件可以支持C/C++。评估下来支持程度最好依次是:scala > java > c++。

安装一个SBT环境

官网下载一个安装包有tgz和zip格式的,如:sbt-1.2.8.zip。 特别点的是需要去配置path,把sbt的安装路径配置到系统path中,这样才可以使用sbt命令。

开发工具intellij idea需要去安装sbt插件,插件名叫Scala。Settings -> Plugins -> sbt -> Scala

plugin-scala

在intellij idea官网有专门安装sbt插件的文档,非常详细&靠谱,有空的兄弟可以好好的去研究一下。

我们如何使用

选择用intellij idea工具开发项目时,我先git项目到本地,在通过idea import项目,这个时候要选择项目构建工具sbt,项目ok后依赖包如下:

build-sbt

build-idea

如何用它日常开发管理项目依赖包

在我的项目中采用gradle和sbt组合管理jar,有个jar配置文件build.gradle.

dependencies {
    play dependencyFor("com.typesafe.play:play-guice", scalaVersion, playVersion)
    play dependencyFor("com.typesafe.play:play-logback", scalaVersion, playVersion)
    play dependencyFor("com.typesafe.play:filters-helpers", scalaVersion, playVersion)
}

添加新jar依赖后,idea 会自动 import并更新,这些书院idea工具的强大能力。

SBT tasks清单

tasks

一直想总结一下sbt工具,也就有了这篇博客的念头,也为是为了以后回顾准备。当时写了一半就是耽误了,再想起来时已经2月过去了,记不起来当时是什么思路写的,感觉有点完成任务的心态。谁让懒是程序员的有点呢!!!

欢迎多提意见

可以关注我的公众号:冬天就要吃胖点

公众号