Skip to content

Commit

Permalink
Adapt plugincache MergeStrategy to sbt-assembly:2.0.0
Browse files Browse the repository at this point in the history
Update MergeStrategy code to use temp files to extract the InputStreams
Update the sbt assembly plugin to use 2.0.0-RC1
Update the log4j version to 2.17.2
Update version to 2.1.0
  • Loading branch information
fnqista committed May 4, 2022
1 parent f5699a3 commit 3b30d60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lazy val commonSettings: Seq[Setting[_]] = Seq(
git.baseVersion in ThisBuild := "2.0.1",
git.baseVersion in ThisBuild := "2.1.0",
organization in ThisBuild := "org.idio"
)

Expand All @@ -12,9 +12,9 @@ lazy val root = (project in file(".")).
description := "sbt assembly plugin merge strategy for log4j2 plugins",
licenses := Seq("MIT License" -> url("https://github.com/idio/sbt-assembly-log4j2/blob/master/LICENSE")),
scalacOptions := Seq("-deprecation", "-unchecked"),
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10"),
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.0.0-RC1"),
libraryDependencies ++= Seq(
"org.apache.logging.log4j" % "log4j-core" % "2.8.1"
"org.apache.logging.log4j" % "log4j-core" % "2.17.2"
),
crossSbtVersions := Seq("0.13.16", "1.2.8"),
crossScalaVersions := Seq("2.11.8", "2.12.10"),
Expand Down
40 changes: 22 additions & 18 deletions src/main/scala/sbtassembly/Log4j2MergeStrategy.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package sbtassembly

import java.io.{FileOutputStream, File}
import org.apache.logging.log4j.core.config.plugins.processor.PluginCache
import sbt.IO
import sbt.io.Using
import sbtassembly.Assembly.JarEntry

import java.io.{FileInputStream, FileOutputStream}
import java.util.UUID
import scala.collection.JavaConverters.asJavaEnumerationConverter

import org.apache.logging.log4j.core.config.plugins.processor.PluginCache

object Log4j2MergeStrategy {
val plugincache: MergeStrategy = new MergeStrategy {
val name = "log4j2::plugincache"
def apply(tempDir: File, path: String, files: Seq[File]): Either[String, Seq[(File, String)]] = {
val file = MergeStrategy.createMergeTarget(tempDir, path)
val out = new FileOutputStream(file)

val aggregator = new PluginCache()
val filesEnum = files.toIterator.map(_.toURI.toURL).asJavaEnumeration

try {
aggregator.loadCacheFiles(filesEnum)
aggregator.writeCache(out)
Right(Seq(file -> path))
val plugincache: MergeStrategy = MergeStrategy("log4j2::plugincache", 2) { dependencies =>
val DatFileExt = ".dat"
val (datFiles, datURIs) = dependencies.map { dep =>
IO.withTemporaryFile(UUID.randomUUID().toString, DatFileExt, true) { datFile =>
IO.transfer(dep.stream(), datFile)
datFile -> datFile.toURI.toURL
}
finally {
out.close()
}.unzip

val stream = IO.withTemporaryFile(UUID.randomUUID().toString, DatFileExt, true) { mergedDatFile =>
Using.bufferedOutputStream(new FileOutputStream(mergedDatFile)) { os =>
val aggregator = new PluginCache()
aggregator.loadCacheFiles(datURIs.toIterator.asJavaEnumeration)
aggregator.writeCache(os)
() => new FileInputStream(mergedDatFile)
}
}
datFiles.foreach(IO.delete)
Right(JarEntry(dependencies.head.target, stream) +: Vector.empty)
}
}

0 comments on commit 3b30d60

Please sign in to comment.