bloo bloo - 2 months ago 14
Scala Question

Using sbt-native-packager, how can I simply prepend a directory to my bash script's ${app_classpath}?

My project uses sbt-native-packager with packageArchetype.java_application.
During sbt stage I have a task that generates some final Typesafe style configuration file that I then copy to:

target/universal/stage/conf/application.conf


I'd like to prepend this directory to the runtime classpath in the bash script, and am looking for the simplest way to do that. I'd hate to maintain a separate src/main/templates/bash-template for something so simple, and am not seeing exactly how to go about it otherwise.

Thanks!

Answer

Short Answer

Define a package mapping

mappings in Universal <+= (packageBin in Compile, sourceDirectory ) map { 
    (_, src) =>
    // we are using the reference.conf as default application.conf
    // the user can override settings here
    val conf = src / "main" / "resources" / "reference.conf"
    conf -> "conf/application.conf"
}

Create a jvmopts in src/universal/conf with

-Dconfig.file=/<installation-path>/conf/application.conf

Add to build.sbt

bashScriptConfigLocation := Some("${app_home}/../conf/jvmopts")

Example for server_archetype: Follow the example application. A bit of description can be found here.

Long answer

Changing the classpath is not supported directly by the sbt-native-packager, because it can cause problems like

  • classpath ordering
  • security issues

Like Typesafe Config, most libraries which use config files, provide a parameter to define the location of the configuration file. Use the parameters describe in the documentation.

It seems your are trying to run a server, which means you can use the

packageArchetype.java_server

which is designed to read external configurations. Take a look at the example application how to use it.

Comments