How to manage multiple interdependent modules with SBT and IntelliJ IDEA?

David Soergel picture David Soergel · Jan 6, 2012 · Viewed 14k times · Source

I'm developing several modules with dependencies among them, and would like to work with them all together in one IDEA project. I'm using sbt-idea to generate IDEA projects from the sbt build definitions, which works great for individual projects. In the multiple-module case, however, the things I've tried so far don't quite work:

Use sbt-idea to generate an IDEA .iml file for each module independently; then create a master IDEA project from scratch an add those modules to it. This makes the module sources all editable in the same window, but the dependencies among them are not tracked (so trying to navigate from some source within the foo project to something in bar takes me to the imported library version of bar, not the local sources).

Use sbt multi-project builds (aka subprojects), where the parent project's Build.scala contains things like:

lazy val foo = Project(id = "foo", base = file("foo"))
lazy val bar = Project(id = "bar", base = file("bar")) dependsOn(foo)

This almost works, in that sbt-idea generates a master IDEA project with the dependencies among the subprojects tracked. There are however two caveats:

  1. It seems to be an sbt restriction that the subprojects must live in subdirectories of the master project (i.e., file("../foo") is not allowed). This is not really what I want (what if a module--such as a "utils" or "commons" package--is used in two different master projects?) but I can live with it.
  2. One of my subprojects has its own subprojects; I'm not sure whether sbt itself deals with these nested projects correctly, but in any case they are ignored by sbt-idea. Obviously I need nested subprojects to be included recursively in the master project.

To summarize: I'd like to collect modules which may already have subprojects into one big IDEA project with tracked dependencies for convenient editing. How can I do it? Thanks!

Answer

Edmondo1984 picture Edmondo1984 · May 9, 2012

The approach with multi-project build is the correct one. You can have a nested tree of subprojects of arbitrary length, but you cannot have a module belonging to multiple parent projects. This makes absolutely sense, and in Maven happens the same.

The reason is that it would be hard to have the same module into multiple projects and keep the sources synchronized. A normal workflow is the following:

  • You have a project which the module belongs to, where you modify the module source.
  • You publish the module into your local repository
  • In other projects where you need the module, you declare it as a libraryDependency

If you want to load a module which does not belong to the current project inside Idea, this is however feasible as you can add this as an external module to the workspace:

  • SBT-IDEA generates the .iml files for your project and you import them in the workspace
  • You can add other.iml of other projects to the workspace
  • If you modify external SBT modules that you have manually added to the workspace, you should republish them to get the changes visibile on the "main" project, which sees those external modules are a "libraryDependency"