36
votes

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?

1

1 Answers

23
votes

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")