Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 413
bloo Avatar asked Dec 02 '22 20:12

bloo


2 Answers

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.

like image 179
Muki Avatar answered Dec 05 '22 10:12

Muki


The following setting:

scriptClasspath in bashScriptDefines ~= (cp => "../conf" +: cp),

Allows you to do exactly what you need.

In this specific example I prepend the "../conf" directory to the classpath entries.

Also, you need to import the following configuration keys to your build SBT:

import com.typesafe.sbt.packager.Keys.bashScriptDefines
import com.typesafe.sbt.packager.Keys.scriptClasspath
like image 28
RoyB Avatar answered Dec 05 '22 09:12

RoyB