Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. When I try to compile the Scala module, only scalac is triggered. It compiles both Java and Scala sources.

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). How to make IDEA use different compiler for different modules?


My workaround is to (for each dependency to Java module):

  1. Delete module dependency in project configuration
  2. Add dependency to appropriate compile output directory "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. I hope somebody knows a better way.


Clarification: I'd like to stress, that tools like SBT or Maven don't solve my problem. It is not about compilation alone. It's about compilation in IDEA, required for things like Scala Worksheet or running unit tests from IDEA. My goal is to have full range of IDEA niceties (syntax highlighting, intelligent auto-completion, auto-imports, etc) with compilation speed of SBT. 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.

like image 757
Przemek Pokrywka Avatar asked Sep 30 '13 13:09

Przemek Pokrywka


1 Answers

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). I was wrong.

In fact, IDEA will pick up classes compiled by any other tool (like the great SBT for instance). You just need to assure that all classes are up-to-date before using any of IDEA's helpful features. The best way to do it is:

  1. launch continuous incremental compilation in the background (for example by issuing "~ compile" in SBT)
  2. remove "make" step in IDEA's run configurations

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. That's the workflow I missed until now! Thanks to everybody for all the comments about the issue.

like image 108
Przemek Pokrywka Avatar answered Nov 15 '22 16:11

Przemek Pokrywka