Time configuration based on environment variable using sbt native wrapper

I use the sbt native packager plugin to create my application's zip file for deployment on an elastic beanstalk. I would like to set environment variables in my beanstalk environment and use them to configure my application at runtime. I tried referencing env variables in my Procfile as follows:

web: ./bin/bridgeservice -Dhttp.port=$PORT

This does not work, because it is $PORTnot interpolated by the start script generated by the packer.

I also tried to define the variables in my build.sbt file as follows:

import scala.util.Properties

javaOptions in Universal ++= Seq(
  "-Dhttp.port=" + Properties.envOrElse("PORT", "9004"),
)

This also does not work, because the packer expects an PORTenv variable during creation of distributed zip and hardcodes with the default value of 9004 in the application.ini file.

java ?

+4
2

javaOptions in Universal conf/application.ini, sbt-native-packager docs . ini :

${app_home}/conf/application.ini . # , . . build.sbt, .

, , env var, :

№1. script

build.sbt:

bashScriptExtraDefines += """addJava "-Dhttp.port=${PORT:-9004}""""

.

№2: JAVA_OPTS env var

JAVA_OPTS script. , AWS ElasticBeanstalk, env vars .

+1

, , .

:

def sysPropOrDefault(propName: String, default: String): String = Option(System.getProperty(propName)).getOrElse(default)
val somePort = sysPropOrDefault("port", "9004")

:

lazy val someProject = project("some-project")
  .enablePlugins(JavaServerAppPackaging)
  .settings(
    javaOptions in Universal ++= Seq(
      s"-Dhttp.port=$somePort"
    )
  )

javaOptions . s .

:

sbt clean update -Dport=9005 docker:publishLocal
0

Source: https://habr.com/ru/post/1666197/


All Articles