Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize Intellij and sbt builds for a scala project

I have an sbt project that I have imported into Intellij. Sometimes I build the project at the command line using sbt, and then when I need to debug I build it from within Intellij. However, each time I alternate it requires a full rebuild when there is no need. Both build procedures output to the same class folder, namely .../target/scala-2.11/classes, so I don't understand why a full rebuild keeps happening?

like image 528
user79074 Avatar asked Jul 22 '17 12:07

user79074


People also ask

How does Scala integrate with IntelliJ?

To install Scala plugin, press Ctrl+Alt+S , open the Plugins page, browse repositories to locate the Scala plugin, click Install and restart IntelliJ IDEA. Now you can successfully check out from VCS, create, or import Scala projects.

How do I get sbt tab in IntelliJ?

Click sbt on the right sidebar to open the tool window. The tool window displays the sbt linked projects, their tasks, dependencies, and all changes made to the underlying build.


2 Answers

As stated by CrazyCoder, intellij and sbt build have each their own tracking of changed files for incremental build. Thus each time one re-compile a file, the other treats it as a changed file and recompile it too.

While CrazyCoder's answer describes how to make them work on separated directory, by changing the sbt compiled classes dir. This answer explain how you can configure Intellij to use sbt for all build, thus only sbt does the compilation. This is a relatively new feature.

Just check the option:

file
  > Settings
    > Build, Execution, Deployment
      > Build Tools
        > SBT 
          > Use SBT shell for build and import

It works at least since intellij version 2017.2.3, and most probably it is an option from the SBT plugin.

For details about this feature, see jetbrains ticket: https://youtrack.jetbrains.com/issue/SCL-10984

like image 74
Juh_ Avatar answered Nov 06 '22 14:11

Juh_


IntelliJ IDEA cannot reuse the classes produced by the other build systems because it has its own incremental compiler which tracks the dependencies and builds the caches during the compilation so that it can compile only modified and dependent files when you make a change in the code. When you built with SBT/Maven/Gradle or command line javac, IntelliJ IDEA compiler cache doesn't know about what has changed and which files it should compile, therefore it performs the full rebuild.

A solution would be to use different output directories for IDE and SBT, this way IntelliJ IDEA will rebuild only files modified since the last build in the IDE and your command line SBT build will not trigger a rebuild in the IDE.

This configuration is performed using the sbt-ide-settings plug-in.

Add the following into plugins.sbt (or whatever files you configure the plugins in):

resolvers += Resolver.url("jetbrains-bintray",url("http://dl.bintray.com/jetbrains/sbt-plugins/"))(Resolver.ivyStylePatterns)
addSbtPlugin("org.jetbrains" % "sbt-ide-settings" % "0.1.2")

And here is how to customize the IDE output directory in build.sbt:

ideOutputDirectory in Compile := Some(new File("target/idea/classes"))
ideOutputDirectory in Test := Some(new File("target/idea/test-classes"))

Feel free to change the paths according to your needs.

like image 26
CrazyCoder Avatar answered Nov 06 '22 16:11

CrazyCoder