3

I have a project with with subprojects added as git submodules in subdirectories, each independent projects having their own build.sbt files. The root projects depends on and aggregates these subprojects. How can I override a setting value (e.g. organization or version) inside those subprojects?

lazy val p1 = (project in file("p1"))
  .settings(organization := "xyz.abc") // This does not work :(

lazy val root = (project in file("."))
  .dependsOn(p1)
  .aggregate(p1)
ɹɐʎɯɐʞ
  • 542
  • 5
  • 15

1 Answers1

2

Try putting state overrides in onLoad which is

of type State => State and is executed once, after all projects are built and loaded.

For example,

lazy val settingsAlreadyOverriden = SettingKey[Boolean]("settingsAlreadyOverriden","Has overrideSettings command already run?")
settingsAlreadyOverriden := false
commands += Command.command("overrideSettings") { state =>
  if (settingsAlreadyOverriden.value) {
    state
  } else {
    Project.extract(state).appendWithSession(
      Seq(
        settingsAlreadyOverriden := true,
        subprojectA / organization := "kerfuffle.org",
      ),
      state
    )
  }
}

onLoad in Global := {
  ((s: State) => { "overrideSettings" :: s }) compose (onLoad in Global).value
}

settingsAlreadyOverriden is necessary for Avoiding recursive onLoad execution #3544

Related questions

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
  • Thank you very much @Marco! This works like a charm. Is there a difference between setting `Global / onLoad` inside of a subproject `settings` versus in the global scope of the main `build.sbt`, except the obvious non-determinism of lazy evaluations? – ɹɐʎɯɐʞ Feb 28 '20 at 00:38