Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way to write multi-project builds with .sbt files in sbt 0.13

Tags:

scala

sbt

I hear .sbt files have been improved in various ways in 0.13, and that now I can specify multi-project builds in them.

http://www.scala-sbt.org/0.13.0/docs/Community/ChangeSummary_0.13.0.html#sbt-format-enhancements mentions that we can now define subprojects in a .sbt file. I also know that multiple .sbt files in the root will be aggregated into a single conceptual file.

What I'd really like, though, is to not pollute my root with a dozen subproject .sbt files. Is there a way I can throw the subproject build.sbt files into their respective subdirectories, keep some common code between them somewhere shared, and then have a root build.sbt for the entire project that aggregates the subprojects? I have a similar setup in .scala files right now but would prefer to use .sbt files if possible.

If that isn't possible, what is the "correct" way to construct large multi-project builds with .sbt files?

like image 727
Mysterious Dan Avatar asked Jul 25 '13 12:07

Mysterious Dan


People also ask

What is project in file in sbt?

A project is defined by declaring a lazy val of type Project. For example, : lazy val util = (project in file("util")) lazy val core = (project in file("core")) The name of the val is used as the subproject's ID, which is used to refer to the subproject at the sbt shell.

What does the src folder contain in an sbt project?

The project folder contains sbt configurations and settings, while src contains all your source code. sbt creates the target folder after you compile your code; it includes the JVM bytecode of your application.


1 Answers

It should already be the case in 0.12 that you can put .sbt files in the base directory of a subproject and the settings there will be included in that project's scope.

Code is reused between .sbt files by creating a normal .scala file in project/. The code in project/ will be available for use in the .sbt files. The definitions in one .sbt are not visible to other .sbt files, at least in 0.13. This is mainly an implementation restriction and it is undetermined whether this will be lifted in future versions.

The default root project will aggregate all subprojects, including those coming from projects defined in subProject/build.sbt.

The current difficulty is making it explicit. For example, the following build.sbt in the root directory would define a subproject in sub/. This is a full definition, defining the ID, base directory, etc... for the project.

<root>/build.sbt

lazy val sub = project 

However, it cannot reference anything defined in <sub>/build.sbt. (The existence of sub/build.sbt is not known until after <root>/build.sbt is compiled and evaluated.) So, to explicitly define what sub aggregates, you'd need something like:

sub/build.sbt

lazy val sub = project.in(file(".")).aggregates(subSub) //or: lazy val sub = project in file(".") aggregate subSub  lazy val subSub = project 

However, this duplicates the definition of sub.

A possible solution going forward is to make the root definition just a reference, like:

<root>/build.sbt

lazy val sub = LocalProject("sub") 
like image 66
Mark Harrah Avatar answered Oct 07 '22 00:10

Mark Harrah