Subproject dependencies in SBT

2019-08-09 18:34发布

问题:

I am having a strange problem with SBT subprojects which I think is dependency related. Here's my setup:

  • I have an SBT project with two subprojects A and B.
  • A contains a class and companion object MyA
  • B depends on A.
  • B contains an object MyB which has a main method.

When I try to execute MyB from the SBT prompt, I get a NoSuchMethodError on MyA. This is not a ClassNotFoundException, but maybe it's happening because it sees the MyA class on the classpath, but not the MyA object.

As a sanity check, I dropped the B subproject and moved its source into the A source tree. When I run MyB from the SBT prompt, it works as expected.

Has anyone run into this, or am I doing something obviously wrong?

Here is my project configuration:

class MyProject(info: ProjectInfo) extends ParentProject(info) {

  lazy val a = project("a", "a", new AProject(_))
  lazy val b = project("b", "b", new BProject(_), a)

  object Dependencies {
    lazy val scalaTest = "org.scalatest" % "scalatest_2.9.0" % "1.4.1" % "test"
  }

  class AProject(info: ProjectInfo) extends DefaultProject(info) with AutoCompilerPlugins {
    val scalaTest = Dependencies.scalaTest
    val continuationsPlugin = compilerPlugin("org.scala-lang.plugins" % "continuations" % "2.9.0")
    override def compileOptions = super.compileOptions ++ compileOptions("-P:continuations:enable") ++ compileOptions("-unchecked")
  }

  class BProject(info: ProjectInfo) extends DefaultProject(info)

}

回答1:

It turns out to have been a problem enabling the continuations plugin on project B. Here's my working configuration:

class MyProject(info: ProjectInfo) extends ParentProject(info) {

  lazy val a = project("a", "a", new AProject(_))
  lazy val b = project("b", "b", new BProject(_), a)

  object Dependencies {
    lazy val scalaTest = "org.scalatest" % "scalatest_2.9.0" % "1.4.1" % "test"
  }

  class AProject(info: ProjectInfo) extends DefaultProject(info) with AutoCompilerPlugins {
    val scalaTest = Dependencies.scalaTest
    val continuationsPlugin = compilerPlugin("org.scala-lang.plugins" % "continuations" % "2.9.0")
    override def compileOptions = super.compileOptions ++ compileOptions("-P:continuations:enable") ++ compileOptions("-unchecked")
  }

  class BProject(info: ProjectInfo) extends DefaultProject(info) with AutoCompilerPlugins {
    override def compileOptions = super.compileOptions ++ compileOptions("-P:continuations:enable") ++ compileOptions("-unchecked")
  }

}