Search code examples
scalaslicktypesafe-config

Read config file while generating Slick code via SBT


I am reading this sample which shows me how to generate source code using Slick-CodeGen

https://github.com/slick/slick-codegen-example/blob/master/build.sbt

And while this sample, is good, I want to modify it so that it reads the database config from application.conf using typesafe config.

Otherwise I will have to replicate the database connection configuration here and also in application.conf file.

Does anyone know, how can this sample be modified so that we can use the typesafe config to read the config values from application.conf?

Edit: Based on the suggestion below, I tried the following

I created a file called build.sbt in the project folder

libraryDependencies += "com.typesafe" % "config" % "1.3.1"

modified my main build.sbt file (in project root) as

val slickVersion = "3.1.1"

lazy val mainProject = Project(
   id = "FooBar",
   base=file("."),
   settings = Defaults.coreDefaultSettings ++ Seq(
      scalaVersion := "2.11.8",
      libraryDependencies ++= Seq(
         "com.typesafe.slick" %% "slick" % slickVersion,
         "com.typesafe.slick" %% "slick-codegen" % slickVersion,
         "mysql" % "mysql-connector-java" % "5.1.35",
         "com.typesafe" % "config" % "1.3.1"
      ),
      myConf := {
         ConfigFactory.parseFile(new File("src/main/resources/application.conf"))
      },
      slick <<= slickCodeGenTask,
      sourceGenerators in Compile <+= slickCodeGenTask
   )
)

lazy val slick = TaskKey[Seq[File]]("gen-tables")
lazy val myConf = settingKey[Config]("The application properties")

lazy val slickCodeGenTask = (sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map {(dir, cp, r, s) =>
   val outputDir = (dir / "slick").getPath
   val username = myConf.value.getString("mysql.username")
   val password = myConf.value.getString("mysql.password")
   val port = myConf.value.getInt("mysql.port")
   val db = myConf.value.getString("mysql.db")
   val server = myConf.value.getString("mysql.server")
   val url = s"jdbc:mysql://$server:$port/$db?username=$username&password=$password"
   val jdbcDriver = myConf.value.getString("mysql.jdbcDriver")
   val slickDriver = myConf.value.getString("mysql.slickDriver")
   val pkg = "sql"
   val fname = outputDir + "/db/Tables.scala"
   toError(r.run("slick.codegen.SourceCodeGenerator", cp.files, Array(slickDriver, jdbcDriver, url, outputDir, pkg), s.log))
   Seq(file(fname))
}

But it cannot resolve the Config and the ConfigFactory classes.


Solution

  • Declare a dependency on Typesafe Config in project/build.sbt:

    libraryDependencies += "com.typesafe" % "config" % "1.3.1"
    

    And define a setting holding your config file in build.sbt:

    lazy val myConf = settingKey[Config]("The application properties")
    
    myConf := {
        ConfigFactory.parseFile(new File("src/main/resources/application.conf"))
    }
    

    Now you can use myConf.value.getString("xyz") to get hold of your configuration values in other tasks or settings.