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

寵の児 提交于 2019-12-02 14:34:51

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"
user2829759

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.

With sbt 13.5 and intellij 13.x, you can specify inter-project dependency with relative path, using a Build.scala. Let's say you have two projects, a core project commons and another project foo, both living in a common directory code/

  1. create Build.scala under code/foo/project/
  2. put this code snippet insde Build.scala

    object ProjectDependencies {
        val commons = RootProject(file("../commons"))
    }
    
    object ProjectBuild extends Build {
        import ProjectDependencies._
    
        lazy val root = Project(id = "foo", base = file(".")).dependsOn(commons)
    }
    
  3. Generate your IntelliJ project via sbt by sbt gen-idea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!