Sbt 如何从 git 中提取依赖项工件?

我听说(我知道我也看到了一些例子,只要我记得在哪里) sbt可以从 git repo 获得依赖关系。

我希望从 github 获得依赖关系 万岁。该存储库不提供任何工件 JAR 文件,只提供一个源树,该源树是用 sbt构建的。我想象的过程是 sbt将下载源回购,构建它,然后使用它作为依赖性工件。

我可能在想象 sbt实际上可以做类似的事情。它可以吗? 如果可以,怎么做?

31657 次浏览

Yes indeed. You can give your Project a dependency with the dependsOn operator, and you can reference a Github project by its URI, for example RootProject(uri("git://github.com/dragos/dupcheck.git")). Alternatively, you can git clone the project, and then reference your local copy with RootProject(file(...)). See "Full Configuration" on the SBT wiki for details and examples.

You can import unpackaged dependencies into your project from GitHub by treating them as project dependencies, using the dependsOn operator. (This is distinct from the way that precompiled library dependencies are included).

Note that you can specify which branch to pull using # notation. Here's some Scala SBT code that is working well for me:

object V {
val depProject = "master"
// Other library versions
}


object Projects {
lazy val depProject = RootProject(uri("git://github.com/me/dep-project.git#%s".format(V.depProject)))
}


// Library dependencies
lazy val myProject = Project("my-project", file("."))
.settings(myProjectSettings: _*)
.dependsOn(Projects.depProject)
.settings(
libraryDependencies ++= Seq(...

Note that if you have multiple SBT projects dependending on the same external project, it's worth setting up a central sbt.boot.directory to avoid unnecessary recompilations (see instructions here).

Since I had problems getting the dependencies of my library resolved (using the suggested RootProject) I'd like to point out to the object called ProjectRef. Thus, if one need to depend on a library residing in git, I suggest to do so as follows:

import sbt.Keys._
import sbt._


object MyBuild extends Build {


lazy val root = Project("root", file("."))
.dependsOn(myLibraryinGit)
.settings(
...,
libraryDependencies ++= Seq(...))


lazy val myLibraryinGit = ProjectRef(uri("git://git@github.com:user/repo.git#branch"), "repo-name")


}

Source: http://blog.xebia.com/git-subproject-compile-time-dependencies-in-sbt/

I wanted to add an answer for sbt 0.13+. Just put something like this to your build.sbt on project root folder (not Build.scala):

lazy val root = (project in file(".")).dependsOn(playJongo)


lazy val playJongo = RootProject(uri("https://github.com/bekce/play-jongo.git"))