igx
igx

Reputation: 4231

sbt how can I compile different configuration files

I would like to compile different config file per deployment I cannot use vm options e.g

-Dconfig.file=test.conf

. How can I define a task e.g : sbt assemblyLocal that will use the config file

src/main/resources/local.conf

and for production I can simply run

sbt assembly

that will use the default application.conf ?

Upvotes: 2

Views: 1145

Answers (1)

himos
himos

Reputation: 56

I'm not sure how to do it with new task but you can define additional configurations and run them like sbt prod:assembly and sbt local:assembly:

lazy val Prod = config("prod") extend(Compile) describedAs("scope to build production packages")
lazy val Local = config("local") extend(Compile) describedAs("scope to build staging packages")

val root = (project in file(".")).configs(Prod, Local)
  .settings(inConfig(Local)(Classpaths.configSettings ++ Defaults.configTasks ++ baseAssemblySettings ++Seq(
    assemblyJarName := "local.jar",
    assemblyMergeStrategy in assembly := {
    case PathList("local.conf") => MergeStrategy.discard
    case x =>
      val oldStrategy = (assemblyMergeStrategy in assembly).value
      oldStrategy(x)
  }
))).settings(inConfig(Prod)(Classpaths.configSettings ++ Defaults.configTasks ++ baseAssemblySettings ++ Seq(
  assemblyJarName := "prod.jar",
  assemblyMergeStrategy in assembly := {
    case PathList("application.conf") => MergeStrategy.discard
    case PathList("local.conf") => new MyMergeStrategy()
    case x =>
      val oldStrategy = (assemblyMergeStrategy in assembly).value
      oldStrategy(x)
  }
)))

And your own MergeStrategy that renames file to application.conf:

import java.io.File

import sbtassembly.MergeStrategy

class MyMergeStrategy extends MergeStrategy{
  override def name: String = "Rename to application.conf"

  override def apply(tempDir: File, path: String, files: Seq[File]): Either[String, Seq[(File, String)]] = {
    Right(files.map(_ -> "application.conf"))
  }
}

Also for a more information you can refer to this article: http://eed3si9n.com/4th-dimension-with-sbt-013 Or this stackoverflow answer: Multiple executable jar files with different external dependencies from a single project with sbt-assembly

Upvotes: 0

Related Questions