sbt配置简洁方便的lib依赖

问题

众所周知在撰写sbt的build.sbt 配置时
随着子项目的和依赖的lib逐渐增加,build.sbt会变得臃肿无比~

方案

在一个sbt项目中,有个文件夹project ,是时候发挥它的作用了

  • 在project 中创建一个文件,不妨就叫做Libs.scala
  • 编辑Libs.scala添加所需的依赖; 例如
import sbt._

object Libs {
  val akkaVersion     = "2.5.2"
  val sparkVersion    = "2.2.0"
  val playVersion     = "2.6.3"

  val slick     = Seq("com.typesafe.slick" %% "slick" % "3.2.0")
  val mysql     = Seq("mysql" % "mysql-connector-java" % "6.0.6")
  val quill     = Seq("io.getquill" %% "quill-jdbc" % "1.3.0")
  object akka {
    val actor   = Seq("com.typesafe.akka" %% "akka-actor" % akkaVersion)
    val remote  = Seq("com.typesafe.akka" %% "akka-remote" % akkaVersion)
    val slf4j   = Seq("com.typesafe.akka" %% "akka-slf4j" % akkaVersion)
    val testkit = Seq("com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test)

    val actors = actor ++ remote ++ slf4j ++ testkit
  }
  object play {
    val json = Seq("com.typesafe.play" %% "play-json" % playVersion )
    val ws   = Seq("com.typesafe.play" %% "play-ws" % playVersion )
  }

  object spark {
    val sql       = List("org.apache.spark" %% "spark-sql" % sparkVersion)
    val core      = List("org.apache.spark" %% "spark-core" % sparkVersion)
    val streaming = List("org.apache.spark" %% "spark-streaming" % sparkVersion)

    val sparks =  sql ++ core ++ streaming
  }

}
  • 切换至build.sbt 可以这么使用
libraryDependencies ++= (
  Nil
    ++ Libs.slick     
    ++ Libs.mysql 
    ++ Libs.spark.sparks
  )

好了现在我们有了干净清爽的build.sbt 配置

你可能感兴趣的:(sbt配置简洁方便的lib依赖)