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

Document log4j2 plugins custom merge strategy #501

Closed
conet opened this issue Jul 19, 2023 · 2 comments
Closed

Document log4j2 plugins custom merge strategy #501

conet opened this issue Jul 19, 2023 · 2 comments

Comments

@conet
Copy link

conet commented Jul 19, 2023

It's really annoying that this does not work without custom code, the sbt-assembly-log4j2 plugin does not work with the new version of the sbt-assembly plugin so here's my contribution for everyone encountering this problem.

assembly / assemblyMergeStrategy := {
  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 x =>
    val oldStrategy = (assembly / assemblyMergeStrategy).value
    oldStrategy(x)
}
@conet
Copy link
Author

conet commented Jul 19, 2023

I know it's ugly but org.apache.logging.log4j.core.config.plugins.processor.PluginCache works only with files it does not work with in memory streams.

@mpollmeier
Copy link

I had the same needs, so I forked, upgraded and released it.
For instructions see https://github.com/mpollmeier/sbt-assembly-log4j2

Special thanks to @fnqista for his PR.

@eed3si9n eed3si9n closed this as completed Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants