简体   繁体   English

如何使IntelliJ IDEA使用javac for Java和scalac for Scala?

[英]How to make IntelliJ IDEA use javac for Java and scalac for Scala?

In my IDEA project a Scala module depends on a Java module. 在我的IDEA项目中,Scala模块依赖于Java模块。 When I try to compile the Scala module, only scalac is triggered. 当我尝试编译Scala模块时,只触发scalac。 It compiles both Java and Scala sources. 它编译Java和Scala源代码。

I'd like scalac to compile only the Scala module, because javac is much faster for Java sources (and my Java project is a big one). 我希望scalac只编译Scala模块,因为javac对Java源代码来说要快得多(而且我的Java项目很重要)。 How to make IDEA use different compiler for different modules? 如何让IDEA为不同的模块使用不同的编译器?


My workaround is to (for each dependency to Java module): 我的解决方法是(对于Java模块的每个依赖项):

  1. Delete module dependency in project configuration 删除项目配置中的模块依赖项
  2. Add dependency to appropriate compile output directory "MyJavaModule/target/classes" 将依赖项添加到适当的编译输出目录“MyJavaModule / target / classes”

Obviously I'm not happy with that, because every time I reimport Maven project I need to repeat all of this to have fast compilation. 显然我对此并不满意,因为每次重新导入Maven项目时,我都需要重复所有这些以便快速编译。 I hope somebody knows a better way. 我希望有人知道更好的方法。


Clarification: I'd like to stress, that tools like SBT or Maven don't solve my problem. 澄清:我想强调,像SBT或Maven这样的工具无法解决我的问题。 It is not about compilation alone. 它不仅仅是编译。 It's about compilation in IDEA, required for things like Scala Worksheet or running unit tests from IDEA. 它是关于IDEA中的编译,对于像Scala Worksheet或从IDEA运行单元测试所需。 My goal is to have full range of IDEA niceties (syntax highlighting, intelligent auto-completion, auto-imports, etc) with compilation speed of SBT. 我的目标是使用SBT的编译速度获得全方位的IDEA细节(语法高亮,智能自动完成,自动导入等)。 Now I have to either tolerate long compilation times (due to dependencies to my Java module) or to use bare-bones REPL and testing in SBT. 现在我必须要么容忍很长的编译时间(由于我的Java模块的依赖性),要么在SBT中使用裸机REPL和测试。

Randall Schulz has asked the right question in the comment: " Why does it matter which tool does the compilation? " Up until now I believed that IDEA needs to compile all classes itself if you want to use its nice features (like IDEA's Scala Console or running tests from within it). Randall Schulz在评论中提出了正确的问题 :“ 为什么编译哪个工具很重要? ”到目前为止,我认为IDEA需要自己编译所有类,如果你想使用它的优秀功能(如IDEA的Scala控制台或从内部运行测试)。 I was wrong. 我错了。

In fact, IDEA will pick up classes compiled by any other tool (like the great SBT for instance). 事实上,IDEA将选择由任何其他工具(例如伟大的SBT)编译的类。 You just need to assure that all classes are up-to-date before using any of IDEA's helpful features. 在使用IDEA的任何有用功能之前,您只需要确保所有类都是最新的。 The best way to do it is: 最好的方法是:

  1. launch continuous incremental compilation in the background (for example by issuing "~ compile" in SBT) 在后台启动连续增量编译(例如在SBT中发出“~compile”)
  2. remove "make" step in IDEA's run configurations 在IDEA的运行配置中删除“make”步骤

That's all! 就这样! You can then use all cool features of IDEA (not only syntax highlighting and code completion, but all auto-imports in Scala Console, quickly running selected unit tests) without switching between different windows. 然后,您可以使用IDEA的所有很酷的功能(不仅语法突出显示和代码完成,而且Scala控制台中的所有自动导入,快速运行选定的单元测试),而无需在不同窗口之间切换。 That's the workflow I missed until now! 那是我到目前为止错过的工作流程! Thanks to everybody for all the comments about the issue. 感谢大家对该问题的所有评论。

You should look at using a dependency management suite like Apache Ivy or Apache Maven. 您应该考虑使用Apache Ivy或Apache Maven等依赖管理套件。 Then put your Java source in a separate artifact, and have your Scala project be dependent on the Java project artifact. 然后将Java源代码放在单独的工件中,让Scala项目依赖于Java项目工件。

If you go the Maven route, there is a Scala plugin . 如果你去Maven路线,有一个Scala插件

Probably the simplest way to get compiled Scala and Java files is SBT - Simple Build Tool. 获取编译的Scala和Java文件的最简单方法可能是SBT - 简单构建工具。 Just create a project (+ add dependencies and so on) and compile it. 只需创建一个项目(+添加依赖项等)并编译它。 Scala + Java compilation works out of the box. Scala + Java编译开箱即用。 I've switched to SBT from Maven. 我已经从Maven转到了SBT。

If you have a complex POM or if you have another reason not to migrate to SBT, you can try to configure the POM. 如果您有复杂的POM,或者您有其他原因不能迁移到SBT,则可以尝试配置POM。 Just adding (and possibly configuring) the Scala plugin should be enough. 只需添加(并可能配置)Scala插件就足够了。 I hope it will not break the Java support. 我希望它不会打破Java的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM