简体   繁体   中英

IntelliJ Idea compiling with SBT by default

Let's say I have a scala code, opening in intellij idea :

object Test extends App {
                           // <- I click here
  def init[T](xs: List[T]) : List[T] = xs match {
    case List() => throw new Error("empty list")
    case List(x) => List() // empty list 
    case head :: tail => head :: init(tail)
  }

  val list = List(1,2,4)

  println ( init(list) )

}

Then, what I do when want to launch this code, I click between lines where object and def is defined to let IDE know what I want to launch (in this case whole object, because I do not select any method). Click CTRL+SHIFT+F10 - to run.

It starts.. I see "Test" in my configuration combo-box... But at that exact moment I stop compilation process .. and go to that configuration to change the config ..

What I change: is "Before launch" section to make it run with "sbt:compile" . I do it because I want to rely on SBT but not on IDE .

The question is: Is there a way to launch/compile in sbt by default in IntellyJ IDEA?

Yup you can do it, but you'll need to make sure this is a valid SBT project. To verify, make sure you can compile and run from SBT on the command line.

  1. Install the SBT plugin for Intellij IDEA
  2. Open the run configuration for this Test in Run -> Edit Configurations...
  3. In the "Before Launch" section remove Make and add the SBT action "Run SBT Action 'compile'"

Now modify your Test code and re-run. You'll see in the status bar that SBT compile is being run before your Test code executes.

Updated To make this the default behavior for all future run configurations, make this change in the Defaults -> Application item in the Edit Configurations... dialog.

Let me update this as this still comes up when searching for a solution. The SBT plugin is now depreciated and we can configure building by SBT from the IntelliJ preferences:

Preferences --> Build, Execution, Deployment --> Build Tools --> sbt: Use SBT shell for imports, for builds

See https://blog.jetbrains.com/scala/2017/03/23/scala-plugin-for-intellij-idea-2017-1-cleaner-ui-sbt-shell-repl-worksheet-akka-support-and-more/ for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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