0

Using the sbt-osgi plugin, one can create 'fat jars' by using the OsgiKeys.embeddedJars attribute.

For example the following code (extracted from this test) embedds every dependency with a name starting with jUnit into the compiled jar:

OsgiKeys.embeddedJars := (Keys.externalDependencyClasspath in Compile).value map (_.data) filter (
  _.getName startsWith "junit")

In my case I have dependencies declared as follows:

libraryDependencies += "org.apache.logging.log4j" % "log4j-api" % "2.7" % Provided
libraryDependencies += "com.typesafe.akka" %% "akka-actor" % akkaVersion

I would like every library to be embedded into the fat jar except for the ones annoated with Provided. So in this case com.typesafe.akka should be included in the fat jar while org.apache.logging.log4j should not be compiled into the jar.

Is there any way to modify the filter method above so that it fulfills my requirement?

Florian Baierl
  • 2,378
  • 3
  • 25
  • 50

1 Answers1

0

I found a way to achieve what I wanted:

OsgiKeys.embeddedJars := (Keys.externalDependencyClasspath in Compile).value.map(_.data).filter(
  file => {
    /*
     * Find configs for file and return false if it includes "test" or "provided"
     */
    libraryDependencies.value.map(x => { (x.name.toLowerCase, x.configurations.map(_.toLowerCase)) })
      .find { case (n, _) => file.getName.toLowerCase.contains(n) }
      .flatMap {case (_, c) => c} match {
      case x if x.contains("test") => false
      case x if x.contains("provided") => false
      case _ => true
    }
  })
Florian Baierl
  • 2,378
  • 3
  • 25
  • 50