2

I have an sbt project where i am building the jar file by running sbt assembly. The issue is there are different config files based for different environment in the src/main/resources directory. For production it's application-prod.conf and for Stage it's application-stage.conf .But in the jar file all the configuration files are getting included. Is there any way i can pass the value from command line while building the assembly jar file ?

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 config files are:-

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

And i build the jar file by:-

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

How can i do something like:-

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

Also if i don't pass anything it should include a default config file.

dks551
  • 1,113
  • 1
  • 15
  • 39
  • 1
    A different approach to your problem is taken here: http://www.scala-sbt.org/sbt-native-packager/recipes/package_configuration.html – peterschrott Sep 29 '17 at 16:04

0 Answers0