3

I'd like to know how to convert a regular scala project into an sbt project. I've tried manually creating an sbt file on the root directory, correctly implemented, but Intellij still doesn't recognize this as a sbt project, i.e, it won't show me in the "View -> Tool Windows" the "SBT" option.

How should I go about this? What I'm actually attempting to do is to create an empty project with multiple (independent) modules.

From what I've gathered there seems to be no way to add a module directly with sbt support, am I right?

Thanks

devoured elysium
  • 101,373
  • 131
  • 340
  • 557
  • Do you want to have a Scala project which has SBT modules or an SBT project with multiple modules? – Alexey Romanov Nov 24 '15 at 23:37
  • Grrr, good question. I have no idea, I'm still infant in this regard. I would probably lean for the former rather than the latter, as I'd like to physically separate the different parts of the project to avoid coupling. Or can I do that in both ways? Would a single SBT project with multiple modules allow me to just checkout from the repository one module and build it? – devoured elysium Nov 24 '15 at 23:51
  • Yes. It's better if you have multiple subproject (a.k.a. a multi-project SBT). Each project will have their own directory, options, settings, etc. and they can be compiled, packaged, tested independently. Projects can have dependencies to external libraries and dependencies to other subprojects. – marios Nov 25 '15 at 06:31

2 Answers2

3

Here is an example of a multi-project build. The root project "aggregates" them all in case you want to compile them all together or package them all together, etc. The "coreLibrary" project depends on the code of "coreA" and "coreB".

import sbt.Keys._
import sbt._

name := "MultiProject"

lazy val root = project.in(file(".")).aggregate(coreA, coreB, coreLibrary)

lazy val coreA = Project("CoreA", file("core-a")).settings(
  organization := "org.me",
  version := "0.1-SNAPSHOT"
)

lazy val coreB = Project("CoreB", file("core-b")).settings(
  organization := "org.me",
  libraryDependencies += "org.apache.kafka" %% "kafka" % "0.8.2-beta",
  version := "0.3-SNAPSHOT"
)

lazy val coreLibrary = Project("UberCore", file("core-main")).dependsOn(coreA, coreB).settings(
  organization := "org.me",
  version := "0.2-SNAPSHOT"
)

You can (for example) compile each project from the command line:

>sbt CoreB/compile

Or you can do this interactively:

>sbt
>project CoreB
>compile
marios
  • 8,874
  • 3
  • 38
  • 62
1

I recommend you to use a single multiple-module SBT project. sbt is a great build tool for scala, you can do a lot of things with sbt, including checking out from the repository one module and built it.

sbt
projects
project <helloProject>

Actually, this feature allows multiple people to work on the same project in parallel. Please take a look at this: http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Multi-Project.html.

Luong Ba Linh
  • 802
  • 5
  • 20