Blog

Git Subproject Compile-time Dependencies in Sbt

26 Jun, 2015

When creating a sbt project recently, I tried to include a project with no releases. This means including it using libraryDependencies in the build.sbt does not work. An option is to clone the project and publish it locally, but this is tedious manual work that needs to be repeated every time the cloned project changes.
Most examples explain how to add a direct compile time dependency on a git repository to sbt, but they just show how to add a single project repository as a dependency using an RootProject. After some searching I found the solution to add projects from a multi-project repository. Instead of RootProject the ProjectRef should be used. This allows for a second argument to specify the subproject in the reposityr.
This is my current project/Build.scala file:

import sbt.{Build, Project, ProjectRef, uri}
object GotoBuild extends Build {
  lazy val root = Project("root", sbt.file(".")).dependsOn(staminaCore, staminaJson, ...)
  lazy val staminaCore = ProjectRef(uri("git://github.com/scalapenos/stamina.git#master"), "stamina-core")
  lazy val staminaJson = ProjectRef(uri("git://github.com/scalapenos/stamina.git#master"), "stamina-json")
  ...
}

These subprojects are now a compile time dependency and sbt will pull in and maintain the repository in ~/.sbt/0.13/staging/[sha]/stamina. So no manual checkout with local publish is needed. This is very handy when depending on an internal independent project/module and without needing to create a new release for every change. (One side note is that my IntelliJ currently does not recognize that the library is on the class/source path of the main project, so it complains it cannot find symbols and therefore cannot do proper syntax checking and auto completing.)

Passionate software developer working for Xebia and organiser at Amsterdam.scala, always looking to learn, share, help, teach and meet new people. Also a serial meetup and conference attendee.
guest
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Joost
Joost
6 years ago

Removing the .idea project data and reimporting the project solved my syntax completion problem

Gus
Gus
6 years ago

Great post! Do you have any idea how to exclude a transitive dependency in the ProjectRef?
My main project (root) has a newer version of a dependency that happens to be included in the ProjectRef, but I can’t seem to evict the older version of this dependency…

blake
blake
5 years ago

Thanks for sharing. This saves my life.

Conrad
Conrad
4 years ago

Great tip, thanks!

Explore related posts