java - intelliJ IDEA 对 sbt 不可变的支持

标签 java intellij-idea sbt immutables-library

使用java时immutables带有 IDEA 和 sbt 的库,编译和运行代码工作正常,但编辑器在使用生成的类时会给出“无法解析符号...”和“无法解析方法...”错误。

已关注 the documentation for setting up IDEs对于 Maven 工作正常,但不能解决 sbt 的问题。

我们如何才能使用 sbt 在 IDEA 上生成的源代码中获得编辑器支持和代码补全?

最佳答案

首先,请按照 the documentation 中的说明进行操作.

To configure annotation processing in IntelliJ IDEA, use dialog Preferences > Project Settings > Compiler > Annotation Processors.

接下来,问题是 sbt 将我们生成的源文件放在 target/scala-2.n/classes/our/package 中。这是编译的目录.class文件,所以我们需要在其他地方生成我们的源。编辑 IDEA 设置对我们没有帮助,因此我们需要编辑 build.sbt通过添加以下内容:

// tell sbt (and by extension IDEA) that there is source code in target/generated_sources
managedSourceDirectories in Compile += baseDirectory.value / "target" / "generated_sources"
// before compilation happens, create the target/generated_sources directory
compile in Compile <<= (compile in Compile).dependsOn(Def.task({
  (baseDirectory.value / "target" / "generated_sources").mkdirs()
}))
// tell the java compiler to output generated source files to target/generated_sources
javacOptions in Compile ++= Seq("-s", "target/generated_sources")

最后我们需要告诉 IDEA 并非 target/ 中的所有内容应通过删除该目录上的排除来忽略。转到文件 > 项目结构 > 项目设置 > 模块,单击 target目录并取消选择“排除”。或者,右键单击target项目选项卡下的目录,将目录标记为 > 取消排除。

此时您应该看到编辑器支持正在工作,如果没有,请运行 sbt clean compile以确保生成源。


更新:<<=最近的 sbt 版本中已删除语法,您可以将上面的第二个指令替换为

// before compilation happens, create the target/generated_sources directory
compile in Compile := (compile in Compile).dependsOn(Def.task({
  (baseDirectory.value / "target" / "generated_sources").mkdirs()
})).value

关于java - intelliJ IDEA 对 sbt 不可变的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42568234/

相关文章:

java - android studio 使 Colors.xml 损坏,然后发生未知错误

intellij-idea - 如何使用实时模板将 $SELECTION$ 转换为蛇形案例(例如)?

intellij-idea - 要导入的文件未找到或 compass 不可读 - IntelliJ

scala - 是否可以在不运行 'sbt clean' 的情况下运行 Scala SBT Scoverage

java - 方法引用属于什么类?

java - 如何使用 Hibernate Session.doWork(...) 进行保存点/嵌套事务?

command - SBT 提供自定义命令的帮助

scala - 如何使用 VS Code 和 Metals 运行现有的 Scala 项目?

java - Android Studio:PC关闭后库的内存不足(Java堆空间)错误

java - ubuntu下Intellij编译android的hello world项目时出错