RP-
RP-

Reputation: 5837

SBT sub projects depending on each other

I have a multi-project SBT build. There is a root which does not have anything, it just aggregates all sub projects.

lazy val aaRoot = (project in file(".")).settings(commonSettings: _*).settings(
  libraryDependencies ++= appDependencies
).enablePlugins(PlayJava).aggregate(foo, bar)

lazy val foo: Project = (project in file("modules/foo")).settings(commonSettings: _*).settings(
  libraryDependencies ++= appDependencies
).enablePlugins(PlayJava).dependsOn(bar)

lazy val bar = (project in file("modules/bar")).settings(commonSettings: _*).settings(
  libraryDependencies ++= appDependencies
).enablePlugins(PlayJava).dependsOn(foo)

It is clearly a cyclic dependency here (foo depends on bar and bar depends on foo). What are the possible approaches to avoid these kinds of dependencies or is there an SBT way of handling this.

Upvotes: 1

Views: 988

Answers (2)

RP-
RP-

Reputation: 5837

As @Augusto suggested, I have re-organized my classes into three different sub-modules and using my Dependency Injection little more wisely. This solved my problem and gave much more abstraction than what I initially had.

Three sub-projects:

  • api (Just interfaces)
  • foo (depends on api)
  • bar (depends on api)
  • aaRoot (which aggregates all of the above)

In FooModule (in my case Guice module), I am binding FooInterface from api module to FooImplementation (foo module). While calling the bar module, I just use BarInterface (from api) by passing FooInterface. Same as the case with bar module.

At runtime they all will be available and it can easily resolve.

Upvotes: 1

Augusto
Augusto

Reputation: 29997

None of the build tools I know allow for cyclic dependencies... and in my experience that is a symptom of a issue with the design of the application or modules, rather than a 'missing' feature from the tools. It's even seen as something bad when this happens at the package level in the same module/jar.

Can you merge those 2 modules? or reshufle the classes so the cyclic dependency disappears?

Upvotes: 5

Related Questions