Sbt nested dependsOn

177 Views Asked by At

I have a root project that depends on a subproject1. And subproject1 depends on subproject2. Does that imply that I Can use subproject2's source code directly in root?

lazy val root =
  Project(id = "root", base = file(".")).dependsOn(sub1)

lazy val sub1 =
  Project(id = "sub1").dependsOn(sub2)

lazy val sub2 =
  Project(id = "sub2")
2

There are 2 best solutions below

0
On BEST ANSWER

Yes.

This can easily be checked.

build.sbt

name := "sbtdemo"

version := "0.1"

ThisBuild / scalaVersion := "2.13.4"

lazy val root =
  Project(id = "root", base = file(".")).dependsOn(sub1)

lazy val sub1 =
  Project(id = "sub1", base = file("sub1")).dependsOn(sub2)

lazy val sub2 =
  Project(id = "sub2", base = file("sub2"))

sub2/src/main/scala/App.scala

object App {
  def foo() = println("foo")
}

src/main/scala/Main.scala

object Main {
  def main(args: Array[String]): Unit = {
    App.foo() // foo
  }
}
0
On

Yes. From Classpath dependencies by sbt:

lazy val core = project.dependsOn(util)

Now code in core can use classes from util. This also creates an ordering between the projects when compiling them; util must be updated and compiled before core can be compiled.