3
votes

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.

1

1 Answers

2
votes

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 += ...
  )