如何使用SBT和IntelliJ IDEA管理多个相互依赖的模块? [英] How to manage multiple interdependent modules with SBT and IntelliJ IDEA?

查看:195
本文介绍了如何使用SBT和IntelliJ IDEA管理多个相互依赖的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发几个相互依赖的模块,并希望在一个IDEA项目中一起使用它们.我正在使用 sbt-idea 从sbt构建定义生成IDEA项目,这非常适合个人项目.但是,在多模块情况下,到目前为止,我已经尝试了一些事情:

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:

使用sbt-idea分别为每个模块生成一个IDEA .iml文件;然后从头开始创建一个主IDEA项目,并向其中添加这些模块.这样一来,模块的所有源代码都可以在同一窗口中进行编辑,但是它们之间的依赖关系不会被跟踪(因此,尝试从 foo 项目中的某个源导航到 bar 中的某个源)将我带到导入的 bar 库版本,而不是本地资源.

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).

使用sbt 多项目构建( aka子项目),其中父项目的Build.scala包含以下内容:

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)

这几乎可以正常工作,因为sbt-idea生成一个主IDEA项目,其中跟踪的子项目之间具有依赖性.但是,有两个警告:

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. 子项目必须位于主项目的子目录中似乎是sbt限制(即,不允许file("../foo")).这不是我真正想要的(如果在两个不同的主项目中使用模块(例如"utils"或"commons"包),该怎么办?)但我可以接受.
  2. 我的一个子项目有自己的子项目;我不确定sbt本身是否正确处理了这些嵌套项目,但是无论如何它们都被sbt-idea忽略.显然,我需要将嵌套子项目以递归方式包含在主项目中.
  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.

总结:我想将可能已经有子项目的模块 收集到一个具有跟踪依赖项的大型IDEA项目中,以便于编辑.我该怎么做?谢谢!

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!

推荐答案

子项目必须存在于主项目的子目录中似乎是sbt限制(即,不允许file("../foo")).这不是我真正想要的(如果在两个不同的主项目中使用了一个模块,例如"utils"或"commons"包,该怎么办?)但我可以接受.

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.

对于sbt 13.5和intellij 13.x,您可以使用 Build.scala 指定具有相对路径的项目间依赖关系. 假设您有两个项目,一个核心项目 common 和另一个项目 foo ,它们都位于同一个目录中 code/

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. 在代码/foo/project/
  2. 下创建Build.scala
  3. 将此代码段放入Build.scala中

  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)
}

  • sbt gen-idea

    这篇关于如何使用SBT和IntelliJ IDEA管理多个相互依赖的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆