Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…tegy merge strategy
  • Loading branch information
lindseydew committed Mar 28, 2024
1 parent 7e853ef commit 307dc7f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 40 deletions.
41 changes: 1 addition & 40 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,46 +51,7 @@ val commonSettings: immutable.Seq[Def.Setting[_]] = List(
),
ThisBuild / assemblyMergeStrategy := {
case "META-INF/MANIFEST.MF" => MergeStrategy.discard
case PathList(ps @ _*) if ps.last equalsIgnoreCase "Log4j2Plugins.dat" =>
import java.io.FileInputStream
import java.io.FileOutputStream
import org.apache.logging.log4j.core.config.plugins.processor.PluginCache

import scala.collection.JavaConverters.asJavaEnumerationConverter

import sbt.io.{IO, Using}
import sbtassembly.Assembly.Dependency

CustomMergeStrategy("Log4j2Plugins") { conflicts =>
val dependencyStreamResource =
Using.resource((dependency: Dependency) => dependency.stream())
val tempDir = "target/log4j2-plugin-cache"
val urls = conflicts
.map { conflict =>
dependencyStreamResource(conflict) { is =>
val file = new File(
s"$tempDir/${conflict.module.get.jarName}-Log4j2Plugins.dat"
)
IO.write(file, IO.readBytes(is))
file.toURI.toURL
}
}
val aggregator = new PluginCache()
aggregator.loadCacheFiles(urls.toIterator.asJavaEnumeration)
val pluginCache = new File(s"$tempDir/Log4j2Plugins.dat")
val pluginCacheOutputStream = new FileOutputStream(pluginCache)
aggregator.writeCache(pluginCacheOutputStream)
pluginCacheOutputStream.close()
Right(
Vector(
JarEntry(
conflicts.head.target,
() => new FileInputStream(pluginCache)
)
)
)
}

case PathList(ps @ _*) if ps.last equalsIgnoreCase "Log4j2Plugins.dat" => sbtassembly.Log4j2MergeStrategy.plugincache
case _ => MergeStrategy.first
},
dependencyOverrides ++= Seq(
Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.5")

addSbtPlugin("com.michaelpollmeier" % "sbt-assembly-log4j2" % "1.1.3")

addSbtPlugin("com.gu" % "sbt-riffraff-artifact" % "1.1.18")

addDependencyTreePlugin

0 comments on commit 307dc7f

Please sign in to comment.