10

The answer at https://stackoverflow.com/a/21605652/1737727 shows how to override one setting for a subproject defined with RootProject from the main project. I was wondering if there is a nice way to do this for multiple settings, and then possibly for multiple subprojects, too, so you don't have to list every combination separately. This would be to prevent proliferation and reduce the chance of forgetting a combination and accidentally having a mismatch of settings.

When not using RootProject, the SBT docs show how to do this with a common sequence of settings:

lazy val commonSettings = Seq(
  organization := "com.example",
  version := "0.1.0",
  scalaVersion := "2.11.8"
)

lazy val core = (project in file("core")).
  settings(commonSettings: _*).
  settings(
    // other settings
  )

lazy val util = (project in file("util")).
  settings(commonSettings: _*).
  settings(
    // other settings
  )

But a RootProject has no method to set its settings. I tried something like the following, according to the answer mentioned above:

lazy val util = RootProject(file("../util"))
commonSettings.map(_.key).foreach(key => key in util := key.value)

but this doesn't seem to be the right approach.

I have looked into using Global or ThisBuild scope, but each subproject sets the settings in its own build.sbt file, which takes precedence over these wider scopes if I understand correctly.

Is there a nice way to do this, or should I just set each setting for each subproject manually? Should I use different scopes, e.g. the subprojects define their settings in Global and the main project in ThisBuild?

Community
  • 1
  • 1
sgvd
  • 3,819
  • 18
  • 31

1 Answers1

0

Well you could do this:

commonSettings.map { s => s.mapKey(Def.mapScope(_.in(util))) }

This creates new Seq[Setting[_]] where each of the setting's scopes is changed to be in the util project. This is a valid entry in an sbt file.

Another option is defining an sbt plugin that you add to both your main projects and in RootProject's own build.sbt

But you may want to consider if you actually need to import the util project as a RootProject instead of a regular subproject.

Justin Kaeser
  • 5,868
  • 27
  • 46
  • 2
    That indeed works, great thanks! I use `RootProject` because the sub project is in a sibling directory of the main project (it is shared by multiple projects) and you can't do `project in file("../util")`. It would be great to learn about a better way, though maybe not wholly on topic for this question. – sgvd Apr 10 '17 at 17:57
  • if `util` doesn't need a build of it's own, you can include by adding to `unmanagedSourceDirectories` directly. – Justin Kaeser Apr 10 '17 at 18:14
  • since sbt 1.5.0, the `.in` syntax is marked as deprecated and asks to use slash syntax. – Avinash Anand May 12 '21 at 10:05