SBT 使用踩坑记录

SBT 本身应该还是一个非常优秀的编译工具。就像Scala 对比于 Java ,SBT 对比于 Maven,如果用的好的话,感觉还是好用太多~~ 但是因为国内网络连接不畅,相关资料介绍比较少的缘故,在国内对很多开发同学来说还是有些陌生了。

SBT 仓库的问题

SBT 是可以复用 Maven 仓库的,但是要合理配置,否则在编译项目的时候,就可能被各种包的下载问题给搞崩溃。

  • 配置 ~/.sbt/repositories 仓库列表,国内也有很多介绍配置该文件的博客,但是有很多都是过时的配置,随着时间的更新,建议参考Delta项目里的问题进行配置 https://github.com/delta-io/delta/blob/master/build/sbt-config/repositories. 使用这个仓库列表还有一个必要是因为Maven中央仓库中可能会缺少一些依赖,或者缺少一些以来的一些版本。例如,在maven中央仓库中, https://repo1.maven.org/maven2/com/simplytyped/sbt-antlr4_2.12_1.0/0.8.3/ 只有 0.8.3 这个版本,但是在Spark 项目中引用的却是 0.8.2 版本,这个组件在插件仓库中 https://scala.jfrog.io/artifactory/sbt-plugin-releases/com.simplytyped/sbt-antlr4/scala_2.12/sbt_1.0/ 里实际上还是维护了 0.8.[0-3] 4个版本,这个就是是一个大坑,不清楚这个组件是不是把之前的历史版本给误删了。

  • 配置 SBT 下载插件,建议直接配置为 SBT的全局插件。

    • ~/.sbt/1.0/plugins/plugins.sbt 文件中增加 addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.1.0-M14")
    • ~/.sbt/0.13/plugins/plugins.sbt 文件中增加 addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.1.0-M12") (PS: 1.1.0-M7 存在明显BUG,请使用更高版本)
    • 不同的SBT版本有对应的 sbt-coursier 版本,sbt在下载该plugin的时候,还会在插件的名称上补充上scala版本sbt版本, 详细的查看: https://repo1.maven.org/maven2/io/get-coursier/
    • 注意: 在Delta项目中配置的该插件版本为 1.0.3, 但是该版本是一个很老的版本,在下载时可能会出现问题。
  • 使用 Maven 代理仓库。配置代理仓库可以在项目中配置,也可以进行SBT全局配置。
    全局配置就是借用 sbt-coursier 的配置,在 ~/Library/Preferences/Coursier/mirror.properties 中配置代理仓库:

central.from=https://repo1.maven.org/maven2
central.to=https://company/nenux

如果想在项目中进行配置,需要配置 resolvers

resolvers := Seq(
      // Google Mirror of Maven Central, placed first so that it's used instead of flaky Maven Central.
      // See https://storage-download.googleapis.com/maven-central/index.html for more info.
//      "gcs-maven-central-mirror" at "https://maven-central.storage-download.googleapis.com/maven2/",
      "nexus" at "https://company/nenux",
      DefaultMavenRepository,
      Resolver.mavenLocal,
      Resolver.file("ivyLocal", file(Path.userHome.absolutePath + "/.ivy2/local"))(Resolver.ivyStylePatterns)
    ),

猜你喜欢

转载自blog.csdn.net/wankunde/article/details/117998586
sbt