3

I've seen near-variants of this question, but not with answers that have all the useful information.

Using sbt 0.13.13 and sbt-assembly 0.14.3, and a multi-project build.sbt based on http://www.scala-sbt.org/0.13/docs/Multi-Project.html like this:

lazy val commonSettings = Seq(
  version := "2.3.0",
  scalaVersion := "2.10.6"
)

lazy val config_jar = (project in file(".")).
  settings(commonSettings: _*).
  settings(
    name := "myapp-config",
    test in assembly := {},
    assemblyJarName in assembly := "myapp-config.jar",
    includeFilter in Compile := "myapp.conf"
  )

lazy val build_jar = (project in file(".")).
  settings(commonSettings: _*).
  settings(
    name := "myapp",
    excludeFilter in Compile := "myapp.conf",
    libraryDependencies += ...
  )

Is this enough configuration to be able to build two separate jars? What exactly are the full sbt commands to build each or both from the command line? The command sbt projects only shows build_jar, so something's missing.

Don Branson
  • 13,631
  • 10
  • 59
  • 101

1 Answers1

3

Few comments:

  • Your projects need to point to different paths. Currently both point to the root (file(".")) directory.

  • Assembly will be available for both projects, so you can call the assembly command from each one.

  • Why use Scala 2.10? At the very least (if you are say on mixed Scala/Java project and stack on Java 7), use Scala 2.11.8.

  • If you want one project to rule them all, you need to have an aggregation project. Calling assembly from root will can assembly in each of the two other projects (thus creating your two jars).

So I would write:

lazy val commonSettings = Seq(
  version := "2.3.0",
  scalaVersion := "2.10.6"
)

lazy val root = (project in file(".")).aggregate(config_jar, build_jar)   

lazy val config_jar = (project in file("config")).
  settings(commonSettings: _*).
  settings(
    name := "myapp-config",
    test in assembly := {},
    assemblyJarName in assembly := "myapp-config.jar",
    includeFilter in Compile := "myapp.conf"
  )

lazy val build_jar = (project in file("build")).
  dependsOn(config_jar).
  settings(commonSettings: _*).
  settings(
    name := "myapp",
    assemblyMergeStrategy in assembly := { file =>
        if(file.startsWith("config\\")) MergeStrategy.discard else MergeStrategy.defaultMergeStrategy(file)
    },
    libraryDependencies += ...
  )
Don Branson
  • 13,631
  • 10
  • 59
  • 101
marios
  • 8,874
  • 3
  • 38
  • 62
  • Scala 2.10 - I'd like to use the latest and greatest. This build is for a Spark app running on Azure. As near as I've been able to tell, their cloud is constrained to Spark 1.6.3, Scala 2.10. It's on my list to get upgraded when possible. – Don Branson Feb 11 '17 at 14:35
  • The project itself is currently single-project. I was hoping to find a way to build two artifacts from the existing single project. I haven't investigated breaking it in two projects to accomplish this, but will if that's the only way to go about it. – Don Branson Feb 11 '17 at 14:37
  • I thought that you might have a Spark constrained and stayed in 2.10. As for the 2 sub-projects, I think thats the best solution for what you need to do. – marios Feb 11 '17 at 17:41
  • This comes pretty close, except that build_jar requires dependsOn(config_jar) in order for all of its tests to run. As a consequence, the files from config_jar are included in build_jar, even though excludeFilter is specified to filter them out. – Don Branson Feb 13 '17 at 21:28
  • From http://stackoverflow.com/questions/39371112/sbt-multi-project-merge-strategy-and-build-sbt-structure-when-using-assembly I learned that I could use the config jar but exclude its contents from the fat jar. I'll update your example to demonstrate, and you can keep or not as you see fit. – Don Branson Feb 14 '17 at 17:48
  • 1
    Cool. Thank you for your help. :) – Don Branson Feb 14 '17 at 19:30