Wednesday, June 8, 2011

Migrating to sbt 0.10: sbt-idea-plugin

In pre-0.10 versions of sbt, you could use sbt-idea-plugin to generate IDEA projects from your SBT files. That plugin doesn't work in 0.10, but here's how to make it happen:

Create a new file ~/.sbt/plugins/project/Build.scala and put the following code in it:

import sbt._

object MyPlugins extends Build {
  lazy val root = Project("root", file(".")) 
                    dependsOn (uri("git://github.com/ijuma/sbt-idea.git#sbt-0.10"))
}

Now restart SBT and type gen-idea (this command replaces the old idea). SBT 0.10 includes any code in this directory in all projects, so any other sbt-0.10 project will immediately get this functionality.

The initial project created by idea-gen will have a separate IDEA module for the project folder. I haven't been able to get IDEA to compile anything while this module is there, so I remove the module and then mark the project as excluded in the main module.

Note that if you later make a change to your .sbt file (e.g. adding a new dependency), you need to bring the IDEA project up-to-date. To do this, open the SBT Console in IDEA, run "update" and then run "gen-idea". Your IDEA project will be brought up to date and IDEA will prompt you to reload the project. Choose 'OK' and you're good to go.

(thanks to Graham Tackley for figuring out the proper way to do this)

Update: Updated the path to ~/.sbt/plugins/project/Build.scala, as it's not necessary to configure the IDEA plugin on a per-project basis.

4 comments:

  1. Hey, did you have this error message? Can't find the reason why i get it

    [error] Missing ideaProjectGroup
    java.lang.IllegalArgumentException
    at org.sbtidea.SbtIdeaPlugin$.setting$1(SbtIdeaPlugin.scala:66)
    at org.sbtidea.SbtIdeaPlugin$.projectData(SbtIdeaPlugin.scala:73)
    at org.sbtidea.SbtIdeaPlugin$$anonfun$2.apply(SbtIdeaPlugin.scala:30)
    at org.sbtidea.SbtIdeaPlugin$$anonfun$2.apply(SbtIdeaPlugin.scala:30)

    ReplyDelete
  2. Hi Andrey,

    No, I'm not sure how that would arise.

    Have you also migrated the old sbt-0.7 build files in your project to sbt-0.10?

    ReplyDelete
  3. hmmmm, I get an error in 0.10.1, do you?

    > exit
    dhiller@thedragon:~/areas2/GHX/TntProto1Server$ sbt10
    [info] Compiling 1 Scala source to /home/dhiller/.sbt/plugins/project/target/scala_2.8.1/classes...
    [error] /home/dhiller/.sbt/plugins/project/Build.scala:5: not found: value dependsOn
    [error] dependsOn (uri("git://github.com/ijuma/sbt-idea.git#sbt-0.10"))
    [error] ^
    [error] one error found
    Project loading failed: (r)etry, (q)uit, or (i)gnore?

    ReplyDelete
  4. shoot, never mind, cut and paste failed me as it created two lines...I should have noticed that earlier.

    ReplyDelete