-->

Scala SBT - sbt-native-packager, how to specify cu

2019-07-13 20:39发布

问题:

How can I specify custom staging directory for multi-project configuration? I'm using sbt-native-packager

Below is the sketch of my multi module configuration. When I stage this project (sbt stage) the files are written to

common/target/universal/stage
app1/target/universal/stage
app2/target/universal/stage

I wan to pass an env variable stageSuffix like this: sbt stage -DstageSuffix=XYZ. This variable should make it stage the project to the following directories:

common/target/universal/stage-XYZ
app1/target/universal/stage-XYZ
app2/target/universal/stage-XYZ

I've tried modifying stagingDirectory in commonSettings, but it doesn't quite work, I'm getting errors like "References to undefined settings".

How can I make this work?

val stageSuffix = sys.props.getOrElse("stageSuffix", ".")

val commonSettings = Seq(
  stagingDirectory := stagingDirectory.value / stageSuffix, // this makes it fail
  libraryDependencies ++= Seq(...))

val common = project.in(file("common"))
  .enablePlugins(JavaAppPackaging)
  .settings(commonSettings: _*)

val app1 = project.in(file("app1"))
  .enablePlugins(JavaAppPackaging)
  .settings(mainClass in Compile := Some("app1.Main"))
  .dependsOn(common)
  .settings(commonSettings: _*)
  .settings(libraryDependencies ++= Seq(...))

val app2 = project.in(file("app2"))
  .enablePlugins(JavaAppPackaging)
  .settings(mainClass in Compile := Some("app2.Main"))
  .dependsOn(common)
  .settings(commonSettings: _*)
  .settings(libraryDependencies ++= Seq(...))

val main = project.in(file("."))
  .dependsOn(common, app1, app2)
  .aggregate(common, app1, app2)
  .settings(commonSettings: _*)

回答1:

I managed to get it working like this:

val stagingDirectoryOverrideOpt = sys.props.get("stageSuffix").map { stageSuffix =>
  stagingDirectory in Universal := {
    val stageFile = (stagingDirectory in Universal).value
    new File(stageFile.getParentFile, s"stage-$stageSuffix")
  }
}

val app1 = project.in(file("app1"))
  .enablePlugins(JavaAppPackaging)
  .settings(mainClass in Compile := Some("app1.Main"))
  .dependsOn(common)
  .settings(commonSettings: _*)
  .settings(stagingDirectoryOverrideOpt.toSeq: _*)
  .settings(libraryDependencies ++= Seq(...))

Hope there is a way to make it prettier