148

I just got started with Scala/LiftWeb/Sbt developing, and I'd like to import a Sbt project in IntelliJ Idea. Actually, I managed to import my project in two different ways:

1) with Maven. I created a Maven project, and of top of that I created a Sbt project, which I then imported in IntelliJ. I could then easily start, stop the jetty server, and do other stuff. But that's not what I want. I want to do the same stuff, just Maven-free. That lead me to

2) with Eclipse. So, I created a new Sbt project (with a little script I wrote, configuring the Sbt project to be a WebProject). I used then the sbt-eclipsify plugin to 'convert' the project for Eclipse, which I then imported in IntelliJ (existing source -> eclipse). But the problems started here: I cannot get the IntelliJ Sbt plugin to work.

Can anyone help me with this?

Leif Wickland
  • 3,473
  • 21
  • 42
ksemeks
  • 1,766
  • 3
  • 15
  • 17
  • For someone who needs a quick answer or if its urgent requirement, just go to github and find a simple scala project. Download and then import in your IDE. You should be able to access the scala project. Happy time – A user Nov 02 '16 at 16:03

7 Answers7

171

There are three basic ways how to create a project - modern versions of IntelliJ can import sbt project out of the box, otherwise you can either use sbt plugin to generate IntelliJ project, or use IntelliJ Scala plugin to create sbt project. Basic features work out of the box using both solutions, some complex builds can have problems, so try other tools to see if it works there.

IntelliJ

IntelliJ IDEA has become so much better these days. The current version (14.0.2) supports sbt projects out of the box with the Scala plugin. Just install the plugin and you should be able to open up Scala/sbt projects without any troubles.

Scala plugin under Plugins in Preferences

With the plugin, just point at a sbt project and IDEA is going to offer you a wizard to open that kind of project.

Import sbt project

IntelliJ Scala Plugin

IntelliJ plugin can be found here http://confluence.jetbrains.com/display/SCA/Scala+Plugin+for+IntelliJ+IDEA or can be installed directoly from within the IDE using Settings -> Plugins dialog. Afterwards one can just do File -> New Project -> Scala -> SBT based. IntelliJ will generate basic build.sbt, download necessary dependencies and open project.

SBT Plugin

Sbt plugin that generate an idea project based on the sbt files can be found here: https://github.com/mpeltonen/sbt-idea

SBT 12.0+ & 13.0+

Simply add addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2") to your build.sbt; no additional resolvers are needed.

Older Versions:

SBT 0.11+

Create and add the following lines to ~/.sbt/plugins/build.sbt OR PROJECT_DIR/project/plugins.sbt

resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

Use gen-idea in sbt to create IDEA project files.

By default, classifiers (i.e. sources and javadocs) of sbt and library dependencies are loaded if found and references added to IDEA project files. If you don't want to download/reference them, use command gen-idea no-classifiers no-sbt-classifiers.


SBT 0.10.1 (according to the plugin author, 0.10.0 won't work!)

Create and add the following lines to ~/.sbt/plugins/build.sbt:

resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"

libraryDependencies += "com.github.mpeltonen" %% "sbt-idea" % "0.10.0"

Use gen-idea sbt task to create IDEA project files.

By default, classifiers (i.e. sources and javadocs) of sbt and library dependencies are loaded if found and references added to IDEA project files. If you don't want to download/reference them, use command gen-idea no-classifiers no-sbt-classifiers.


SBT 0.7

To use it, simply run this from your sbt shell, it will use the plugin as an external program:

 > *sbtIdeaRepo at http://mpeltonen.github.com/maven/
 > *idea is com.github.mpeltonen sbt-idea-processor 0.4.0
 ...
 > update
 ...
 > idea
 ...

You can also add trait in your project definition, as you want:

import sbt._
class MyProject(info: ProjectInfo) extends ParentProject(info) with IdeaProject {
  lazy val mySubProject = project("my-subproject", "my-subproject", new DefaultProject(_) with IdeaProject)
   // ...
}
Steve Gury
  • 13,568
  • 6
  • 34
  • 41
  • 4
    I'm a total convert to this plugin. Use it in combination with the idea-sbt-plugin, which lets you delegate your project build to SBT, and use SBT interactively from a tool window in IDEA. Version 0.2.0-SNAPSHOT of sbt-idea fixes a few little annoyances -- creation of a parent project, and preservation of manual IntelliJ configuration. – retronym Nov 23 '10 at 13:57
  • 4
    I retract my answer. This is a great plugin and I now use it every time. – Synesso Dec 06 '10 at 04:57
  • 1
    I added an answer for SBT 0.10.0, which is completely different to SBT 0.7.x+ http://stackoverflow.com/questions/4250318/how-to-create-sbt-project-with-intellij-idea/6519574#6519574 – opyate Jun 29 '11 at 11:02
  • I only had to add this to `project/plubins.sbt` addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.1.0") – Doug Donohoe Sep 19 '12 at 03:00
  • see this - http://pkmishra.github.com/productivity/2013/01/16/scala-sbt-and-intellij-idea-boilerplate/ – Pradeep Kumar Mishra Jan 17 '13 at 15:10
  • http://stackoverflow.com/questions/15750287/sbt-version-and-scala-version-project-configuration-for-intellij-idea-with-sbt – ses Apr 02 '13 at 00:20
  • I use gen-idea to create the sbt project, then I try to import it into IntelliJ, then I get "The project file '' already exists. Would you like to overwrite it? ??? – dranxo Aug 06 '13 at 21:36
  • What am I doing wrong? My `sbt update` finds the `com.github.mpeltonen#sbt-idea;1.5.2 ..` JAR, but fails on `sbt gen-idea`: **[error] Not a valid key: gen-idea**. sbt 0.13.0 – Kevin Meredith Dec 06 '13 at 04:17
  • I think it would be good to include that there is already IntelliJ IDEA plugin which can open/create sbt project directly from inside the IDE. So basically there are two choices - either generate project using sbt plugin or use IntelliJ plugin to work with sbt directly. – Tibor Blenessy Aug 28 '14 at 10:03
  • Intellij has deprecated this functionality. Default projects are no longer created since 2017.2.1 https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000488424-SBT-project-does-not-auto-create-source-directories-anymore-2017-2-1-. As per this issue https://youtrack.jetbrains.com/issue/SCL-12478 you would need to vote to get this functionality re-added. –  Apr 15 '19 at 13:09
11

For now I do this by hand. It is quite simple.

  1. Create the project with SBT
  2. Create a new IDEA Project with the same root path
  3. Create a module with the same root path
  4. Set src/main/scala as a src path on the module
  5. Set src/test/scala as a test path on the module
  6. Add scala-library.jar as a library
  7. Add lib (if it is present) as a jar directory within a module library
  8. Add lib_managed/compile (if it is present) as a jar directory within a module library
  9. Add lib_managed/test (if it is present) as a jar directory within a module library

That's it from memory. It would be better if it were automated, but it's no big deal as it is now.

One note of caution: The above approach doesn't work well with new-school sbt, i.e. versions 0.10 and newer, because it doesn't copy dependencies into lib_managed by default. You can add

retrieveManaged := true

to your build.sbt to make it copy the dependencies into lib_managed.

Leif Wickland
  • 3,473
  • 21
  • 42
Synesso
  • 34,066
  • 32
  • 124
  • 194
  • But, do you start your project (jetty) form IntelliJ or from a terminal? I cannot set up the project launcher/compiler from the IDE. – ksemeks Nov 23 '10 at 04:30
  • One more thing: I did everything you wrote, but IntelliJ is saying "Cannot resolve the symbol ne" for the import "import net.liftweb.LiftRules" – ksemeks Nov 23 '10 at 04:34
  • I managed to got around the import. I had to import /lib_managed/compile instead of /lib_managed – ksemeks Nov 23 '10 at 04:59
  • 6
    Answer retracted! Use the sbt-idea plugin. – Synesso Dec 06 '10 at 04:58
7

Tempus fugit and IntelliJ IDEA has become so much better these days. It's 2015 after all, isn't it?

Having said that, the latest version of IntelliJ IDEA 14.0.2 supports sbt projects out of the box with the Scala plugin. Just install the plugin and you should be able to open up Scala/sbt projects without much troubles.

Scala plugin under Plugins in Preferences

I'm using the Early Access version of the plugin which is 1.2.67.6.EAP as of the time of the writing.

With the plugin just point at a sbt project and IDEA is going to offer you a wizard to open that kind of project.

Import sbt project

About sbt-idea in sbt 0.12.4

For sbt 0.12.4 the system-wide plugin configuration file - ~/.sbt/plugins/build.sbt or PROJECT_DIR/project/plugins.sbt - should have the following lines:

resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin(dependency="com.github.mpeltonen" % "sbt-idea" % "1.5.0-SNAPSHOT")

Run sbt gen-idea to generate IDEA project files.

Read the sbt-idea plugin website for more up-to-date information. You may also find my blog entry Importing sbt-based project to IntelliJ IDEA 13 (with sbt-idea, Scala 2.11 and sbt 0.12) useful.

Jacek Laskowski
  • 64,943
  • 20
  • 207
  • 364
  • When creating new project. After sbt gen-idea the project is empty. No structure, not src, test folder. And it is lame.. – ses Oct 17 '13 at 21:26
  • How did you create the project? What's the directory structure and the files inside? – Jacek Laskowski Oct 19 '13 at 23:35
  • It is just empty. I had to crate it " main / scala / src " by hands. But I can live with that. – ses Oct 20 '13 at 01:58
  • You may want to use https://github.com/n8han/giter8 as a means of bootstrapping an sbt project. sbt can have an empty directory as the main project and main/scala/src is *not* needed - just place your Scala files in the main directory for sbt to pick them. – Jacek Laskowski Oct 20 '13 at 11:33
5

For sbt 0.7

See the answer elsewhere on this page.

For sbt 0.10

Clone and build Ismael's sbt-idea:

git clone https://github.com/ijuma/sbt-idea.git
cd sbt-idea
git checkout sbt-0.10
./sbt package

Create an sbt plugin lib directory if you don't have one already

mkdir -p  ~/.sbt/plugins/lib

Copy the jar built in step one into here

cp sbt-idea/target/scala-2.8.1.final/*.jar ~/.sbt/plugins/lib

Restart or reload sbt, then you can run gen-idea (or gen-idea with-classifiers if you want sources and javadoc in intelliJ too)

Source: Tackers' suggestion on the message group.

In IntelliJ IDEA 13.x itself

You can open an SBT-based project in IDEA nowadays. It will create the necessary project and modules, and keep your dependencies up-to-date whenever you make changes to the build scripts.

Community
  • 1
  • 1
opyate
  • 5,039
  • 1
  • 34
  • 60
2

The answers are old for 2014. In IntelliJ 13.x, the plugin Scala is ver 0.41.2 ( SBT is included).

My SBT version is 0.13.5 (terminal : sbt sbtVersion )

Go to the project's root folder and enter in the terminal

sbt idea

You will see two new hidden folders .idea and .idea_modules.

Then in IntelliJ, File > Open > select the project. It should open the project without any problem.

Raymond Chenon
  • 8,990
  • 10
  • 64
  • 97
2

I just went through all this pain. I spend days trying to get an acceptable environment up and have come to the conclusion that ENSIME, SBT and JRebel are going to be my development environment for some time. Yes, it is going back to Emacs, but ENSIME turns it into a bit or an idea with refactoring, debugging support, navigation, etc. It's not nowhere near as good as Eclipse (Java), but unless the scala plugins work better it's the best we have.

Until the Scala development environments get up to snuff (Eclipse or IntelliJ) I'm not going to bother. They're just way too buggy.

See the discussion on the lift site.

http://groups.google.com/group/liftweb/browse_thread/thread/6e38ae7396575052#

Within that thread, there is a link to a HOWTO for IntelliJ, but although it kinda works, there are many issues that render it a little less that useful.

http://blog.morroni.com/2010/07/14/setup-intellij-9-for-lift-framework-development/comment-page-1/

andyczerwonka
  • 4,090
  • 5
  • 32
  • 55
  • I already read it. This article uses maven, which i managed to get working. I'm trying to do the same now, just with sbt, withot maven. – ksemeks Nov 23 '10 at 04:35
  • I can set up the project for IntelliJ to use it, but cannot get to work the sbt compiler/launcher. – ksemeks Nov 23 '10 at 04:41
  • As far as ENSIME is concerned: is it easy to get working? i tried once, but i never worked with emacs, so it was a little hard for me. is working with emacs a premise? – ksemeks Nov 23 '10 at 04:43
  • The following link will take you through an SBT setup. This worked for me, but again working with SBT and IntelliJ in a non-integrated fashion is really like using two seperate tools, which ENSIME with SBT is much more natural. http://heikoseeberger.blogspot.com/2010/08/how-to-setup-scala-project-with-sbt-and.html – andyczerwonka Nov 23 '10 at 16:16
0

Before you start creating your SBT project, make sure that the Scala plugin is downloaded and enabled in IntelliJ IDEA.

below link explains everything you need to know.

https://www.jetbrains.com/help/idea/2016.1/getting-started-with-sbt.html

Prasad
  • 556
  • 5
  • 11