【问题标题】:Intellij analysis not picking up unmanagedSourceDirectories after sbt importIntellij 分析在 sbt 导入后没有拾取 unmanagedSourceDirectories
【发布时间】:2019-09-05 11:22:41
【问题描述】:

如果我定义了一个依赖于两个外部源文件夹的 sbt scala 项目,则分析无法正常工作。所以说我定义了以下 build.sbt:

lazy val root = project.in(file("."))
  .settings(
    name := "repro",
    version := "1.0",
    scalaVersion := "2.11.8",
    unmanagedSourceDirectories in Compile +=
            baseDirectory.value / ".." / "ext1" /  "src" / "main" / "scala",
    unmanagedSourceDirectories in Compile +=
      baseDirectory.value / ".." / "ext2" /  "src" / "main" / "scala"
  )

这样 ext1 中的源依赖于 ext2 中的源。所以在这个例子中,我在 ext1 中定义了一个 trati T1,在 ext2 中定义了一个依赖于 T1 的 trait T2。我的项目中有一个依赖于 T2 的课程。这将在 sbt 中编译得很好。但是当我在 IntelliJ 中导入这个 sbt 项目时,它会编译。但是,当我在编辑器中打开我的 trait T2 时,当我引用 T1 时它给了我一个错误,说“无法解析 ext1”。为什么会出现此错误?

这个问题的复现可以在github上找到,链接如下:

https://github.com/hughgearse/repro

https://github.com/hughgearse/ext1

https://github.com/hughgearse/ext2

【问题讨论】:

    标签: scala intellij-idea sbt


    【解决方案1】:

    ext1/build.sbt中创建构建定义

    lazy val root = project.in(file("."))
      .settings(
        name := "ext1",
        version := "1.0",
        scalaVersion := "2.11.8"
      )
    

    然后通过RootProject 引用ext1 作为ext2/build.sbt 中的外部构建

    val ext1 = RootProject( file("../ext1") )
    
    lazy val root = project.in(file(".")).dependsOn(ext1)
      .settings(
        name := "ext2",
        version := "1.0",
        scalaVersion := "2.11.8"
      )
    

    然后在 repro/build.sbt 中类似地引用两者作为外部构建

    val ext1 = RootProject( file("../ext1") )
    val ext2 = RootProject( file("../ext2") )
    
    lazy val root = project.in(file(".")).dependsOn(ext1, ext2)
      .settings(
        name := "repro",
        version := "1.0",
        scalaVersion := "2.11.8",
        unmanagedSourceDirectories in Compile +=
                baseDirectory.value / ".." / "ext1" /  "src" / "main" / "scala",
        unmanagedSourceDirectories in Compile +=
          baseDirectory.value / ".." / "ext2" /  "src" / "main" / "scala"
      )
    

    重新导入repro 项目,IntelliJ 应该可以分析所有源代码了。

    【讨论】:

    • 不幸的是,这不起作用,因为在我的真实示例中,ext1 和 ext2 实际上也是完整的项目。他们使用 scala 2.12 和我的 repro scala 2.11(无法更改)。所以当我使用dependsOn时,我的sbt中出现以下错误:找不到模块:default#ext1_2.11;0.1-SNAPSHOT
    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2020-08-11
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多