简体   繁体   English

Scala build.sbt:解决依赖关系递归

[英]Scala build.sbt: Solving dependency recursions

I just started with SBT's Multi-Project-Builds and I ran into an interesting problem that I have not seen a good example for in the scala-sbt docs. 我刚开始使用SBT的Multi-Project-Builds ,却遇到了一个有趣的问题,在scala-sbt文档中没有看到一个很好的例子。

In my build.sbt, Project B and C are dependent on A, but B is also dependent on C (at least dependent on B's classes in C's testing scope): 在我的build.sbt中,项目B和C依赖于A,但是B也依赖于C(至少依赖于C的测试范围内的B的类):

(Common is referenced to an Object in root/project/Common.scala) (在root / project / Common.scala中将公共引用为对象)

root/build.sbt: 根/ build.sbt:

lazy val prjA: Project = project.in(file("Project-A")).
settings(
    name := "Project-A",
    version := Common.prjVersion,
    scalaVersion := Common.scalaVersion,
    libraryDependencies ++= Common.Imports.compileDependencies,
    libraryDependencies ++= Common.Imports.testDependencies,
)

lazy val prjB: Project = project.in(file("Project-B")).
settings(
    name := "Project-B",
    version := Common.prjVersion,
    scalaVersion := Common.scalaVersion,
    libraryDependencies ++= Common.Imports.compileDependencies,
    libraryDependencies ++= Common.Imports.testDependencies,
).dependsOn(prjA)//.dependsOn(prjC % "test->compile")

lazy val prjC: Project = project.in(file("Project-C")).
settings(
    name := "Project-C",
    version := Common.prjVersion,
    scalaVersion := Common.scalaVersion,
    libraryDependencies ++= Common.Imports.compileDependencies,
    libraryDependencies ++= Common.Imports.testDependencies,
).dependsOn(prjA).dependsOn(prjB)        

This build.sbt, as it is written here, runs successful (via sbt clean update compile) but for sure, I cannot start the test-cases in prjB. 如本文所述,该build.sbt运行成功(通过sbt clean update编译),但是可以肯定的是,我无法在prjB中启动测试用例。 Once I establish the .dependsOn(prjC % "test->compile") on prjB in my build.sbt, the output is a StackOverflowError - this makes perfectly sense to me, as the cross-dependency between prjB and prjC can not be solved. 有一次,我建立.dependsOn(prjC % "test->compile")prjB我build.sbt,输出是StackOverflowError -这使得完美的意义,我,作为prjB和prjC之间的交叉依赖关系不能得到解决。

However, is there a practical way to solve this endless recursion? 但是,是否有解决此无限递归的实用方法? I am thinking about one more step in the building process (1 & 2 are done by the actual build.sbt, as you can see), but I don't know how to do that. 我正在考虑构建过程中的另一步骤(如您所见,1和2由实际的build.sbt完成),但是我不知道该怎么做。

  1. First compile prjB with prjA dependency, 首先用prjA依赖项编译prjB,
  2. Then compile prjC with prjA and prjB dependency 然后使用prjA和prjB依赖项编译prjC
  3. at last include the builded prjC's classes in prjB for testing purposes. 最后,将已构建的prjC的类包括在prjB中以进行测试。 <- is this a valid approach? <-这是有效的方法吗?

Best regards and thanks in advance! 最好的问候,并在此先感谢!

This isn't really an SBT problem, but a module dependency problem. 这实际上不是SBT问题,而是模块依赖性问题。 Ignoring A for a moment, since B needs C to compile and C needs B to compile, this cycle cannot be resolved by any build system. 暂时忽略A,由于B需要C进行编译,而C需要B进行编译,因此任何构建系统都无法解决该循环问题。

The only way to solve this is to change the structure of the modules themselves. 解决此问题的唯一方法是更改​​模块本身的结构。 For example, if possible, you could create a D project that contains the common classes and have them both rely on it. 例如,如果可能,您可以创建一个包含公共类的D项目,并使它们都依赖于此。 Or, use the Dependency Inversion Principle . 或者,使用Dependency Inversion Principle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM