idea模块依赖重复_如何使用SBT和IntelliJ IDEA管理多个相互依赖的模块?

博主正在尝试在一个IDEA项目中集成多个具有依赖关系的子项目。目前使用sbt-idea为每个独立项目生成IDEA配置,但无法正确处理多模块间的依赖。尝试了手动创建主项目并添加子项目,以及使用sbt的多项目构建,但遇到了子项目必须在主项目子目录下以及嵌套子项目未被识别的问题。解决方案是使用sbt 13.5及IntelliJ 13.x,通过在Build.scala中指定相对路径实现跨项目依赖。
摘要由CSDN通过智能技术生成

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:

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.

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!

解决方案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/

create Build.scala under code/foo/project/

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)

}

Generate your IntelliJ project via sbt by sbt gen-idea

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值