Search code examples
sbtdebsbt-native-packager

Adding /etc/<application> to the classpath in sbt-native-packager for debian:package-bin


So I'm using the packageArchetype.java_server and setup my mappings so the files from "src/main/resources" go into my "/etc/" folder in the debian package. I'm using "sbt debian:package-bin" to create the package

The trouble is when I use "sbt run" it picks up the src/main/resources from the classpath. What's the right way to get the sbt-native-packager to give /etc/ as a resource classpath for my configuration and logging files?

plugins.sbt:

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.7.0-M2")

build.sbt

...

packageArchetype.java_server

packageDescription := "Some Description"

packageSummary := "My App Daemon"

maintainer := "Me<[email protected]>"

mappings in Universal ++= Seq(
   file("src/main/resources/application.conf") -> "conf/application.conf",
   file("src/main/resources/logback.xml") -> "conf/logback.xml"   
)

....

Solution

  • I took a slightly different approach. Since sbt-native-packager keeps those two files (application.conf and logback.xml) in my package distribution jar file, I really just wanted a way to overwrite (or merge) these files from /etc. I kept the two mappings above and just added the following:

    src/main/templates/etc-default:

     -Dmyapplication.config=/etc/${{app_name}}/application.conf
     -Dlogback.configurationFile=/etc/${{app_name}}/logback.xml
    

    Then within my code (using Typesafe Config Libraries):

    lazy val baseConfig = ConfigFactory.load //defaults from src/resources
    
    //For use in Debain packaging script. (see etc-default)
    val systemConfig = Option(System.getProperty("myapplication.config")) match {
      case Some(cfile) => ConfigFactory.parseFile(new File(cfile)).withFallback(baseConfig)
      case None => baseConfig
    }
    

    And of course -Dlogback.configuration is a system propety used by Logback.