SBT Native Packager:使用不同的配置构建相同的包

SBT Native Packager: Build the same package with different configs

我正在尝试在 SBT Native Packager 的帮助下使用不同的配置文件创建构建。我有一个 Scala 项目的标准项目布局:

my-app
--/project
----/build.properties
----/plugins.sbt
--/src
----/main
------/java
------/resources
------/scala
----/test
------/java
------/resources
------/scala
build.sbt

所以我将 sbt-native-packager 插件添加到我的项目中并决定重复 SBT submodule approach

这就是我的 build.sbt 文件的样子:

import sbt.Keys._

scalaVersion in ThisBuild := "2.12.1"

lazy val app = project
  .in(file("."))
  .settings(
    name := "backend",
    version := "1.0",
    libraryDependencies ++= {
      val akkaVersion = "2.4.17"
      val akkaHttpVersion = "10.0.5"
      Seq(
        "com.typesafe.akka" %% "akka-actor" % akkaVersion,
        "com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
        "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion
      )
    }
  )

lazy val devPackage = project
  .in(file("build/dev"))
  .enablePlugins(JavaAppPackaging)
  .settings(
    name := "backend-dev",
    resourceDirectory in Compile := (resourceDirectory in (app, Compile)).value,
    mappings in Universal += {
      ((resourceDirectory in Compile).value / "dev.conf") -> "conf/application.conf"
    }
  )
  .dependsOn(app)

这里是 application.conf 和 dev.conf(两者都位于 src/main/resources:

akka {
  loglevel = INFO
  stdout-loglevel = INFO
  loggers = ["akka.event.slf4j.Slf4jLogger"]
  default-dispatcher {
    fork-join-executor {
      parallelism-min = 8
    }
  }
  http {
    server {
      server-header = "PinPoint REST API"
      request-timeout = "10.seconds"
    }
  }
}

database {
  dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
  properties = {
    databaseName = "pg_db"
    user = "alex"
    password = ""
  }
  numThreads = 10
}

include "application"

database {
  dataSourceClass = "org.postgresql.ds.PGSimpleDataSource"
  properties = {
    databaseName = "pg_db_dev"
    user = "alex"
    password = "secure_password"
  }
  numThreads = 10
}

在终端中 运行 sbt devPackage/stage 之后,我在项目的根文件夹中获得了新目录 "build"。但在里面: build/dev/target/universal/stage/ "bin" 文件夹中没有 运行 可用的 sh 脚本。

那么如何解决这个问题?

如果未设置 mainClass 设置,Universal Packager 将不会创建启动脚本 - 并且 sbt 不会从类路径中的 jar 文件自动为您设置它。

您需要做的就是将设置添加到您的 devPackage 项目中:

lazy val devPackage = project
  .in(file("build/dev"))
  .enablePlugins(JavaAppPackaging)
  .settings(
    name := "backend-dev",
    resourceDirectory in Compile := (resourceDirectory in (app, Compile)).value,
    // TODO: Use real classname here:
    mainClass in Compile := Some("my.main.ClassName"),
    mappings in Universal += {
      ((resourceDirectory in Compile).value / "dev.conf") -> "conf/application.conf"
    }
  )
  .dependsOn(app)