chiappone
chiappone

Reputation: 2908

How to get Intellij to use dependencies from SBT scala

I am trying to figure out how idea will recognize thrid party dependencies when using SBT. When I use the sbt plugin gen-idea it seems to download all the necessary dependencies which get put into my ~/.ivy/ directory as expected. How can intellij use these deps?

EDIT: One thing I noticed is if I make a new idea project instead of just a module then this works? Any idea why this would be? I would like to be able to have multiple sbt modules in the same project.

Upvotes: 4

Views: 3240

Answers (2)

David Soergel
David Soergel

Reputation: 1699

If you want multiple SBT modules in one IDEA project, you can use sbt multi-project builds (aka subprojects). Just create a master project that refers to the modules as sub-projects, then run gen-idea on the master. To specify dependencies among the modules you have to use Build.scala (not build.sbt), as in jxstanford's answer or like this:

lazy val foo = Project(id = "foo", base = file("foo"))
lazy val bar = Project(id = "bar", base = file("bar")) dependsOn(foo)

One level of subprojects works fine (with the dependencies correctly reflected in the resulting IDEA project), but nested subprojects don't seem to work. Also, it seems to be an sbt restriction that the subprojects must live in subdirectories of the master project (i.e., file("../foo") is not allowed).

See also How to manage multiple interdependent modules with SBT and IntelliJ IDEA?.

Upvotes: 0

jxstanford
jxstanford

Reputation: 3387

The sbt-idea plugin works with multi-module sbt project. We have been using it since somewhere around sbt-0.10.0, and currently are at sbt-0.11.2. It seems like you have the dependency part of the build file set up ok, so here's an example of how we do the project setup from a full specification Build.scala file:

object Vcaf extends Build {
  import Resolvers._
  import Dependencies._
  import BuildSettings._

  lazy val vcafDb = Project(
    id = "vcaf-db",
    base = file("./vcaf-db"),
    dependencies = Seq(),
    settings = buildSettings ++ /* proguard */ SbtOneJar.oneJarSettings ++  Seq(libraryDependencies := dbDeps, resolvers := cseResolvers)
  )

  lazy val vcaf = Project(
    "vcaf",
    file("."),
    dependencies = Seq(vcafDb),
    aggregate = Seq(vcafDb),
    settings = buildSettings ++ Seq(libraryDependencies := vcafDeps, resolvers := cseResolvers) ++ webSettings
  )
}

In the example, the vcaf-db project is in the a folder within the vcaf project folder. The vcaf-db project does not have it's own build.sbt or Build.scala file. You'll notice that we are specifying libraryDependencies for each project, which may or may not be your missing link.

As ChrisJamesC mentioned, you need to do a "reload" from within SBT (or exit sbt and come back in) to pick up changes to your build definition. After the project is reloaded, you should be able to do a "gen-idea no-classifiers no-sbt-classifiers" and get an intellij project that has the main project, modules, and library access as defined in the build file.

Hope it helps!

Upvotes: 3

Related Questions