scala Intellij 编译失败:“已定义为”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16885489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intellij compile failures: "is already defined as"
提问by Nick
I've got a scala project that compiles, runs and tests fine when using SBT from the command line. However, when building the project in intellij, it seems every class in the project has this error in the event log, causing the build to fail:
我有一个 Scala 项目,当从命令行使用 SBT 时,它可以很好地编译、运行和测试。但是,在intellij中构建项目时,似乎项目中的每个类在事件日志中都有这个错误,导致构建失败:
SendCommandToService is already defined as case class SendCommandToService
case class SendCommandToService(service: String, commandName: String, keys: Array[String], values: Array[String])
^
采纳答案by Max
It means there are two compiled classes with identical package and class name found in your classpath. One compiled by sbt, one compiled by IntelliJ.
这意味着在您的类路径中可以找到两个具有相同包和类名的已编译类。一个由 sbt 编译,一个由 IntelliJ 编译。
One of the following should be able to solve the issue:
以下方法之一应该能够解决问题:
- try to generate IntelliJ .iml file with sbt-idearather than import directly.
- sbt clean before click Build -> Rebuild in IntelliJ
- when rebuilding with IntelliJ, make sure sbt is not running
- 尝试使用sbt-idea生成 IntelliJ .iml 文件,而不是直接导入。
- 在单击 Build -> 在 IntelliJ 中重建之前 sbt clean
- 使用 IntelliJ 重建时,请确保 sbt 未运行
回答by jiangok
For me, the reason is that both myproject/srcand myproject/src/main/scalaare marked as Source. So intellij failed to build myproject/src/main/scaladue to above errors. Unmark Source from myproject/src(in intellij, File->Project structure, select myproject Module, select src folder in Sources Tab, remove it from Source in the "Add Content Root" pane) solved the problem. Hope this helps.
对我来说,原因是myproject/src和myproject/src/main/scala都被标记为来源。因此,myproject/src/main/scala由于上述错误,intellij 无法构建。取消标记源myproject/src(在intellij,文件->项目结构中,选择myproject模块,在源选项卡中选择src文件夹,在“添加内容根”窗格中将其从源中删除)解决了问题。希望这可以帮助。
回答by Baztoune
I had the same problem and @Max is right, there is a conflict with the compiled classes, but the solution provided didn't work for me. It turns out that I was using sbt-idea to generate the IDEA project structure as a workaround of an Intellij IDEA 14 + scala plugin bugon the SBT import, that is not fixed yet at the time I write this.
我遇到了同样的问题,@Max 是对的,与编译的类存在冲突,但提供的解决方案对我不起作用。事实证明,我使用 sbt-idea 生成 IDEA 项目结构作为解决SBT 导入上的Intellij IDEA 14 + scala 插件错误的方法,在我撰写本文时尚未修复。
In order to fix it, I had to remove src_managed/main/controllerthat was in conflict with src_managed/mainin the Module settings because of an sbt-idea bug. So double-check your module source folders and make sure you don't have subfolders in conflict with a parent folder already declared as source.
为了修复它,我不得不删除由于sbt-idea bug与模块设置src_managed/main/controller冲突src_managed/main的内容。因此,请仔细检查您的模块源文件夹,并确保您没有与已声明为源的父文件夹冲突的子文件夹。
回答by 425940330
You need to change "Settings -> Build,Execution,Deployment -> Scala Compiler -> Compile order" from "Mixed" to "Java then Scala". If you have compile the project previous, you should first run "sbt clean".
您需要将“Settings -> Build,Execution,Deployment -> Scala Compiler -> Compile order”从“Mixed”更改为“Java then Scala”。如果你之前已经编译过这个项目,你应该首先运行“sbt clean”。
回答by user3603546
I had a similar issue repeatedly both within Idea and without: plain SBT.
我在 Idea 内部和外部都反复遇到类似的问题:普通 SBT。
It turned out that CVS stores copies of some *.scalafiles in subdirectory CVS/Base, which SBT apparently tries to compile. The problem went away when I deleted the CVS subdirectories.
事实证明,CVS 将一些*.scala文件的副本存储在子目录CVS/Base 中,SBT 显然试图编译该子目录。当我删除 CVS 子目录时,问题就消失了。
回答by angelcervera
In my case, the problem was the protobuf Idea plugin:
就我而言,问题出在 protobuf Idea 插件上:
- Remove the idea protbuf plugin.
- Close Idea
- Remove all folders related with idea (.idea and .idea_modules)
- Open Idea and Import the project again.
- 删除idea protbuf 插件。
- 关闭创意
- 删除所有与idea相关的文件夹(.idea和.idea_modules)
- 打开 Idea 并再次导入项目。
回答by Reeebuuk
After the sbt compileI had to mark the folder as Generated Sources Rootbecause I needed those files for compilation.
在sbt 编译之后,我必须将文件夹标记为Generated Sources Root,因为我需要这些文件进行编译。
回答by mercurial
I'll just add mine to the list in case anyone else made this beginner mistake: I temporarily "saved my progress" by doing cp Foo.scala Foo-save.scala, forgetting that sbt would try to compile all the .scalafiles in the directory.
我只是将我的添加到列表中,以防其他cp Foo.scala Foo-save.scala人犯了这个初学者错误:我通过执行临时“保存了我的进度” ,忘记了 sbt 会尝试编译.scala目录中的所有文件。
(I don't know, I guess I was thinking of programming languages where any file not explicitly included was ignored ...)
(我不知道,我想我在考虑编程语言,其中include忽略任何未明确d 的文件......)
Of course, since both the main file and the "temporary backup" file defined the same classes ... yeah.
当然,由于主文件和“临时备份”文件定义了相同的类......是的。

