-->

Setting a module project in Scala as an sbt projec

2019-05-26 22:04发布

问题:

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

回答1:

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


回答2:

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.