In an SBT multi project build, how to reference projects from other folders?

时间:2018-09-19 08:30:08

标签: scala sbt

I am trying to tidy up my huge SBT multi-project build. Currently, I have everything in one big SBT file, but I am trying to fan this out into several directories.

In a nutshell, this is my folder structure:

root
+-foo
| +-foofoo
| | +-build.sbt
| +-build.sbt
| |
| +-foobar
| | +-build.sbt
| +-build.sbt
| |
| bar
| +-barfoo
| | +-build.sbt
| +-build.sbt
| |
+-build.sbt

In root/build.sbt I have:

lazy val root = project.
  in(file(".")).
  settings(publishArtifact := false).
  aggregate(foo,
            bar)

lazy val foo = project in file("foo")
lazy val bar = project in file("bar")

In foo/build.sbt I have:

lazy val foo = project.
  in(file(".")).
  settings(publishArtifact := false).
  aggregate(foofoo,
            foobar)

lazy val foofoo = project in file("foofoo")
lazy val foobar = project in file("foobar")

In bar/build.sbt I have:

lazy val bar = project.
  in(file(".")).
  settings(publishArtifact := false).
  aggregate(barfoo)

lazy val barfoo = project in file("barfoo")

Now, I want barfoo to depend on foofoo, so, naïvely, I defined in barfoo/build.sbt:

lazy val barfoo = project.in(file(".")).dependsOn(foofoo)

But when running sbt compile it complains it can't find foofoo.

So, how to correctly reference these intra-project dependencies?

Currently the only way I can think of is to run sbt foo/publishLocal first and then add this as libraryDependencies. Is this the only way or is there another one?

Followup 1

After some digging, I read that ProjectRef should do the trick, but this gives me another error:

I have some common dependencies in a Dependencies.scala object in my project subfolder. This is found by all subprojects. However when I define:

lazy val foofoo = ProjectRef(file("foo/foofoo"), "foofoo")
lazy val barfoo = project.in(file(".")).dependsOn(foofoo)

I get this:

C:\Somewhere\bar\barfoo\build.sbt:1: error: not found: object Dependencies
import Dependencies._
       ^

Followup 2

I got it to work but I don't know if this is the right approach, so I leave it here but don't post it as an answer:

lazy val foofoo = project in file("../../foo/foofoo")
lazy val barfoo = project.in(file(".")).dependsOn(foofoo)

1 个答案:

答案 0 :(得分:0)

好吧,您已经或多或少已经自己回答了,但是这是我的处理方式:

lazy val core = RootProject(file("../../../myCoreProject/"))
lazy val main = Project(
   id = "mySubProject", 
   base = file(".")
).dependsOn(core)

其中core是我要引用的项目。该文件位于子项目的build.sbt文件中,该文件位于文件树下方的三个目录中。

请参阅:RootProject