How can I read data from a file in a task and use it to set another parameter?
We are porting our application to the Play Framework . We are working with gradle build system and are facing several issues with sbt.
We are using jooq for our database, which means our build file must contain the database username / password (since jooq generates code by reading the db schema). Since this is not a good idea, all sensitive data is stored in a secure file on each host that can potentially run, and the build system reads from the file and then configures the system accordingly. It was pretty easy in gradle, but I hit deadend with sbt. This is what I have so far:
import org.json4s._ import org.json4s.native.JsonMethods. val jsonBuildConfig = TaskKey[JValue]("json-build-config") jsonBuildConfig := { val confLines = scala.io.Source.fromFile("/etc/application.conf").mkString parse(confLines) } jooqOptions := Seq( "jdbc.driver" -> "org.postgresql.Driver", "jdbc.url" -> "FIXME", "jdbc.user" -> "FIXME", "jdbc.password" -> "FIXME" )
The problem is that three configuration parameters must be selected from a file with FIXME
as their current values ββin jooqOptions
.
Internally, jsonBuildConfig
I can do this:
val confLines = scala.io.Source.fromFile("/etc/application.conf").mkString val jsonConf = parse(confLines) (jsonConf / "stagingdb" / "url").values
But how to set it in jooqOptions
conf set?
source to share
If I understand your question correctly, you want the value to jooqOptions
depend on the value jsonBuildConfig
. There is a section on what is here:
http://www.scala-sbt.org/0.13.5/docs/Getting-Started/More-About-Settings.html
Basically you want to use <<=
instead :=
to install jooqOptions
eg.
jooqOptions <<= jsonBuildConfig.apply { jsonConf =>
val dbSettings = jsonConf / "stagingdb"
val dbUrl = dbSettings / "url"
val dbUser = ...
...
Seq("jdbc.driver" -> "...", "jdbc.url" -> dbUrl, ...)
}
For newer SBT versions, you can avoid the boilerplate setting.apply{...}
by calling setting.value
within the initializer block for parameters, e.g.
jooqOptions := {
val dbSettings = jsonBuildConfig.value / "stagingdb"
...
}
I have linked to the docs for SBT 0.13.5 which supports the feature .value
. Double check which version of SBT you are using and open the appropriate docs page to see if it supports this feature.
source to share