简体   繁体   English

如何为scala项目同步Intellij和sbt构建

[英]How to synchronize Intellij and sbt builds for a scala project

I have an sbt project that I have imported into Intellij. 我有一个我导入Intellij的sbt项目。 Sometimes I build the project at the command line using sbt, and then when I need to debug I build it from within Intellij. 有时我使用sbt在命令行构建项目,然后当我需要调试时,我在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? 两个构建过程都输出到同一个类文件夹,即... / target / scala-2.11 / classes,所以我不明白为什么完全重建会继续发生?

As stated by CrazyCoder, intellij and sbt build have each their own tracking of changed files for incremental build. 正如CrazyCoder所说,intellij和sbt 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. 虽然CrazyCoder的答案描述了如何使它们在分离的目录上工作,但是通过更改sbt编译的类dir。 This answer explain how you can configure Intellij to use sbt for all build, thus only sbt does the compilation. 这个答案解释了如何配置Intellij使用sbt进行所有构建,因此只有sbt才能进行编译。 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. 它至少从intellij版本2017.2.3起作用,并且很可能是SBT插件中的一个选项。

For details about this feature, see jetbrains ticket: https://youtrack.jetbrains.com/issue/SCL-10984 有关此功能的详细信息,请参阅jetbrains ticket: https ://youtrack.jetbrains.com/issue/SCL-10984

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. IntelliJ IDEA不能重用其他构建系统生成的类,因为它有自己的增量编译器,它跟踪依赖关系并在编译期间构建缓存,以便在代码中进行更改时,它只能编译已修改的和依赖的文件。 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. 当您使用SBT / Maven / Gradle或命令行javac构建时,IntelliJ IDEA编译器缓存不知道已更改的内容以及应编译的文件,因此它执行完全重建。

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. 解决方案是为IDE和SBT使用不同的输出目录,这样IntelliJ IDEA将仅重建自IDE上次构建以来修改的文件,并且命令行SBT构建不会在IDE中触发重建。

This configuration is performed using the sbt-ide-settings plug-in. 使用sbt-ide-settings插件执行此配置。

Add the following into plugins.sbt (or whatever files you configure the plugins in): 将以下内容添加到plugins.sbt (或您配置插件的任何文件)中:

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 : 以下是如何在build.sbt自定义IDE输出目录:

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. 您可以根据自己的需要随意更改路径。

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

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