如何在程序集中包含特定于环境的配置文件

时间:2017-09-27 19:07:14

标签: sbt sbt-assembly sbt-native-packager

我有一个sbt项目,我通过运行sbt程序集来构建jar文件。问题是src / main / resources目录中有不同的环境,有不同的配置文件。对于生产它的应用程序prod.conf和Stage它的application-stage.conf。但是在jar文件中包含所有配置文件。有什么办法可以在构建程序集jar文件时从命令行传递值吗?

sbt: -

    // Create a new MergeStrategy for aop.xml files
    val aopMerge = new sbtassembly.MergeStrategy {
      val name = "aopMerge"
      import scala.xml._
      import scala.xml.dtd._

      def apply(tempDir: File, path: String, files: Seq[File]): Either[String, Seq[(File, String)]] = {
        val dt = DocType("aspectj", PublicID("-//AspectJ//DTD//EN", "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"), Nil)
        val file = MergeStrategy.createMergeTarget(tempDir, path)
        val xmls: Seq[Elem] = files.map(XML.loadFile)
        val aspectsChildren: Seq[Node] = xmls.flatMap(_ \\ "aspectj" \ "aspects" \ "_")
        val weaverChildren: Seq[Node] = xmls.flatMap(_ \\ "aspectj" \ "weaver" \ "_")
        val options: String = xmls.map(x => (x \\ "aspectj" \ "weaver" \ "@options").text).mkString(" ").trim
        val weaverAttr = if (options.isEmpty) Null else new UnprefixedAttribute("options", options, Null)
        val aspects = new Elem(null, "aspects", Null, TopScope, false, aspectsChildren: _*)
        val weaver = new Elem(null, "weaver", weaverAttr, TopScope, false, weaverChildren: _*)
        val aspectj = new Elem(null, "aspectj", Null, TopScope, false, aspects, weaver)
        XML.save(file.toString, aspectj, "UTF-8", xmlDecl = false, dt)
        IO.append(file, IO.Newline.getBytes(IO.defaultCharset))
        Right(Seq(file -> path))
      }
    }

    assemblyMergeStrategy in assembly := {
      case m if m.toLowerCase.endsWith("manifest.mf") =>MergeStrategy.discard
      case m if m.toLowerCase.matches("meta-inf.*\\.sf$") => MergeStrategy.discard
      case m if m.toLowerCase.matches("meta-inf.*\\.properties") => MergeStrategy.discard
      case PathList("META-INF", "aop.xml") => aopMerge
      case PathList(ps @ _*) if ps  .last endsWith ".txt.1" => MergeStrategy.first
      case "reference.conf"    => MergeStrategy.concat
      case "application.conf"  => MergeStrategy.concat
      case "logback.xml"  => MergeStrategy.concat
      case x =>
        val oldStrategy = (assemblyMergeStrategy in assembly).value
        oldStrategy(x)
    }
    unmanagedSourceDirectories in Compile += baseDirectory.value / "src" / "main" / "gen-java"

应用程序配置文件是: -

application-prod.conf
application-dev.conf
application-stage.conf

我通过以下方式构建jar文件: -

sbt "set test in assembly := {}" clean assembly

我该怎么做: -

sbt env=prod "set test in assembly := {}" clean assembly

此外,如果我没有传递任何内容,它应该包含默认配置文件。

0 个答案:

没有答案