Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling sub projects in sbt

Tags:

scala

sbt

I have some sub-projects those I need to compile with sbt. The structure is like this:

main_project
 - sub_project1
 - sub_project2
 - sub_project3

Of course, they have the correct directory hierarchy (src-main-scala....). How do I compile all of them and each of them in particular?

like image 614
Alan Coromano Avatar asked Aug 31 '13 13:08

Alan Coromano


People also ask

What is ThisBuild in sbt?

Typically, if a key has no associated value in a more-specific scope, sbt will try to get a value from a more general scope, such as the ThisBuild scope. This feature allows you to set a value once in a more general scope, allowing multiple more-specific scopes to inherit the value.

Which is the correct way to add dependencies in sbt file?

Library dependencies can be added in two ways: unmanaged dependencies are jars dropped into the lib directory. managed dependencies are configured in the build definition and downloaded automatically from repositories.

Does sbt run compile?

sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file.

How do we specify library dependencies in sbt?

You can use both managed and unmanaged dependencies in your SBT projects. If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your SBT project, and SBT will find them automatically.


2 Answers

See Navigating projects interactively:

At the sbt interactive prompt, type projects to list your projects and project <projectname> to select a current project. When you run a task like compile, it runs on the current project. So you don't necessarily have to compile the root project, you could compile only a subproject.

You can use aggregate to compile them all.

like image 152
Eugene Yokota Avatar answered Sep 25 '22 05:09

Eugene Yokota


Because the selected answer's linked documentation has changed, the answer isn't so clear anymore.

In the build.sbt of OP's main_project, to compile them together they will have to define an aggregate, as the new(er) documentation links to:

lazy val root = (project in file("."))
  .aggregate(util, core)

lazy val util = (project in file("util"))

lazy val core = (project in file("core"))

And then running an sbt compile in the root project's directory will compile them all.

However, if you wish to compile one at a time, you can navigate your cmd line/terminal to the root project ( main_project here) and then run the following command:

user:main_project$: sbt <project name> / compile

to use the poster's example:

user:main_project$: sbt sub_project1 / compile

(if you are in the SBT terminal, then you can omit sbt)

like image 38
NateH06 Avatar answered Sep 21 '22 05:09

NateH06