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

时间:2017-05-10 11:07:56

标签: scala sbt sbt-native-packager

我正在尝试在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”。但在里面: 建的/ dev /目标/通用/台/ 没有带有可运行sh脚本的“bin”文件夹。

那么如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

如果未设置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)