Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt plugincache MergeStrategy to sbt-assembly:2.0.0 #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 21 additions & 18 deletions src/main/scala/sbtassembly/Log4j2MergeStrategy.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
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.{BufferedInputStream, 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 BufferedInputStream(new FileInputStream(mergedDatFile))
}
}
Right(JarEntry(dependencies.head.target, stream) +: Vector.empty)
}
}