Scala SBT - sbt-native-packager,如何指定自定义阶段目录

时间:2016-06-11 20:18:36

标签: scala sbt sbt-native-packager

如何为多项目配置指定自定义暂存目录?我正在使用sbt-native-packager

下面是我的多模块配置草图。当我暂存此项目(sbt stage)时,文件将写入

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

我想传递一个env变量stageSuffix,如下所示:sbt stage -DstageSuffix=XYZ。此变量应使项目暂存到以下目录:

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

我已经尝试修改stagingDirectory中的commonSettings,但它不起作用,我收到错误,例如"对未定义设置的引用&#34 ;。

我该如何做到这一点?

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 个答案:

答案 0 :(得分:1)

我设法让它像这样工作:

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(...))

希望有办法让它漂亮