使用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/