I have my application.conf that use env variables,
when I run my Play Scala application, I want to specify from witch file I need to export variables, for example: .loc.env or .dev.env ...
My .loc.env file looks like:
VAR_KEY1=value1
VAR_KEY2=value2
I don't want to create another application.conf
I use sbt 1.0 and Playframework 2.6
So my questions are:
How can i export env variable with sbt?
How can check env variable from sbt shell?
envVars in IntegrationTest := Map("imageTag" -> sys.env.getOrElse("imageTag", imageTag.value))
You can reference ENV variables directly in application.conf for example like this:
slick.dbs.default.profile="slick.jdbc.MySQLProfile$"
slick.dbs.default.db.driver="com.mysql.jdbc.Driver"
slick.dbs.default.db.url=${?MYSQL_LINK}
slick.dbs.default.db.user = "root"
slick.dbs.default.db.password = ${?MYSQL_PASS}
Related
I have been trying to set up environment variables directly from build.sbt file as I need to use an assembly jar name which is defined in this file. So I've been trying to output the result of defined environment variable using echo and in the context of the Scala application code sys.props.get("SPARK_APP_JAR_PATH") but the result is blank or None, respectively.
What can be wrong with the environment variable configuration?
This how I defined the env variable in build.sbt:
assemblyJarName in assembly := "spark_app_example_test.jar"
mainClass in assembly := Some("example.SparkAppExample")
test in assembly := {}
fork := true
val envVars = sys.props("SPARK_APP_JAR_PATH") = "/path/to/jar/file"
That's how it works for me:
lazy val dockerRepo: String = sys.props.getOrElse("DOCKER_REPO", s"bpf.docker.repo")
Or with your example:
lazy val envVars = sys.props.getOrElse("SPARK_APP_JAR_PATH", "/path/to/jar/file")
According to the documentation:
System properties can be provided either as JVM options, or as SBT arguments, in both cases as -Dprop=value. The following properties influence SBT execution.
e.g. sbt -DSPARK_APP_JAR_PATH=/path/to/jar/file
See https://www.scala-sbt.org/release/docs/Command-Line-Reference.html#Command+Line+Options
I read that SBT has functionality to generate source code and resource files.
In my case I want to add/modify a field in an application.conf file during compilation/packaging of the project (leaving the others in place)
For instance my application.conf file has something like:
A {
B = "Some Value"
C = "Some value to be modified"
}
I would like in the SBT to read an external file and change or add the value of A.B or A.C
So if it is possible to do something along the lines of:
build.sbt
lazy val myProject = project.in(file('myproject')
// pseudo code - How do I do this?
.sourceGenerators in Compile += "Read file /path/to/external/file and add or replace the value of application.conf A.B = some external value"
You can replace the values with environment variable values provided while compiling / building your project. For that you'd have to
A {
B = "Some Value"
B = ${?B_ENV}
C = "Some value to be modified"
C = ${?C_ENV}
}
Where B_ENV and C_ENV are the environment variables you set in your terminal either before build or within the build command (before it)
$ B_ENV=1 C_ENV=2 sbt run
Source: https://www.playframework.com/documentation/2.6.x/ProductionConfiguration#using-environment-variables
In this case you can do without sbt and this approach would also work with maven or cradle.
The *.conf support orignates from typesafe config (https://github.com/lightbend/config).
There is a feature to get environment variables to be used in the configuration which should be a good fit to solve the problem.
There are two approaches I would suggest to use
1.) Fail on missing configuration
If configuration of this vallue is important and to prevent the deplyment of misconfigurated application the startup should fail on missing environment variables.
in application.conf
key=${TEST} // expects "TEST" to be set, fails otherwise
2.) Hardcoded value with override
If there is a sensible default behaviour that only in some circumstances should be changed.
in application.conf
key="test" // hardcoded key
key=${?TEST} // override "key" with 3nv "$TEST" value, when it is given
Play 2.6.x Scala
I have a default application.conf within the folder {project}/conf/ but I'd like to override some values depending on the environment by passing in the respective file as command-line arguments (as detailed in the docs):
sbt run -Dconfig.file=/conf/qa.conf or sbt run -Dconfig.resource=qa.conf
But I'm not able to get play to pick up the overrides. Here's my file directory:
application
|- playApp1
|- playApp2
|-- conf
|-- application.conf
|-- qa.conf
My build.sbt makes playApp2 the default project on load. And I have confirmed that the defulat application.conf is working -- just the override is not.
Thanks for any ideas!
--
Update
Here are the HOCON files play uses. application.conf
platform {
scheme = "http"
host = "localhost:8080"
}
and the overrides as provided in qa.conf
include "application.conf"
platform {
scheme = "https"
host = "ea311.34.com"
}
Your question is about HOCON, in case you did not realize it.
Without seeing your application.conf I can only provide a generic answer. Here is an example of providing a default value for akka.log-config-on-start, which will be overridden by a Java system property or an environment variable called CONFIG_DUMP, if defined:
akka {
log-config-on-start = false
log-config-on-start = ${?CONFIG_DUMP}
}
This feature of HOCON is documented here.
This works if you provide the command line argument first
sbt -Dconfig.resource=qa.conf run
I have a project that uses the xsbt-web-plugin but I want to be able to customise the jetty settings. Is there a way I can specify my own jetty.xml file? I found the
PluginKeys.configurationFiles
setting and set that to the desired file but it had no effect
You can specify a jetty.xml config file using the config argument to jetty():
jetty(config = "etc/jetty.xml")
If you need to specify multiple config files, you can pass them as individual --config arguments:
jetty(args = Seq
"--config", "jetty.xml"
, "--config", "jetty-https.xml"
, "--config", "jetty-ssl.xml"
))
Check out the docs for more info.
I use xsbt-web-plugin 2.0.2(the latest version) at present.You can do in build.sbt as following:
enablePlugins(JettyPlugin)
containerConfigFile in Compile := Some(file("./src/main/webapp/WEB-INF/jetty-env.xml"))
I hope it can be helpful for you.
I have a subproject named oppenheimer in my project. It's very simple to run this project from the sbt console.
[myproject] $ oppenheimer/run
I can also pass in a command line argument as such:
[myproject] $ oppenheimer/run migrate
[myproject] $ oppenheimer/run clean
How can I do this from build.sbt? Is it possible to define a task that does this? It would suffice to have something like this:
val customMigrate = ...
val customClean = ...
And this is so that I could use it elsewhere in the project, like such:
(test in Test) <<= (test in Test).dependsOn(customMigrate)
The answer is given in the sbt FAQ section "How can I create a custom run task, in addition to run?". Basically:
lazy val customMigrate = taskKey[Unit]("custom run task")
fullRunTask(customMigrate, Test, "foo.Main", "migrate")