Search code examples
scalasbtsbt-native-packager

SBT Native Packager: Build the same package with different configs


I'm trying to create builds with deferent configuration files with help of SBT Native Packager. I have a standard project layout for Scala projects:

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

So I added the sbt-native-packager plugin to my project and decided to repeat the SBT submodule approach.

That's how my build.sbt file looks like:

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)

And here are application.conf and dev.conf (both are located in 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
}

and

include "application"

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

After I run sbt devPackage/stage in the terminal I get new directory "build" in the root folder of the project. But inside of the: build/dev/target/universal/stage/ There is no "bin" folder with runable sh script.

So how to fix this?


Solution

  • The Universal Packager won't create startup scripts if the mainClass setting isn't set - and sbt won't auto-set it for you from jar files in your classpath.

    All you need to do is add the setting to your devPackage project:

    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)