|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | | -import com.github.jengelman.gradle.plugins.shadow.ShadowStats |
3 | | -import com.github.jengelman.gradle.plugins.shadow.transformers.CacheableTransformer |
4 | 2 | import com.github.jengelman.gradle.plugins.shadow.transformers.DontIncludeResourceTransformer |
5 | | -import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext |
6 | | -import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext.Companion.getEntryTimestamp |
7 | | -import org.apache.commons.io.output.CloseShieldOutputStream |
8 | 3 | import org.apache.logging.log4j.core.config.plugins.processor.PluginCache |
9 | | -import org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor |
10 | | -import org.apache.tools.zip.ZipEntry |
11 | | -import org.apache.tools.zip.ZipOutputStream |
12 | 4 | import java.net.URL |
13 | 5 | import java.util.Collections |
14 | | -import java.util.Enumeration |
| 6 | +import java.util.jar.JarFile |
15 | 7 |
|
16 | 8 | version = providers.gradleProperty("VERSION_NAME").get() |
17 | 9 | val jarName = "package-toolkit-runtime" |
@@ -66,6 +58,12 @@ java { |
66 | 58 | tasks { |
67 | 59 | shadowJar { |
68 | 60 | dependsOn("genPklConnectors") |
| 61 | + // Merge log4j plugins ourselves, since shadow seems completely incapable of doing it itself |
| 62 | + dependsOn(mergeLog4jPlugins) |
| 63 | + exclude("META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat") |
| 64 | + from(layout.buildDirectory.dir("log4j-plugins")) { |
| 65 | + into("META-INF/org/apache/logging/log4j/core/config/plugins") |
| 66 | + } |
69 | 67 | isZip64 = true |
70 | 68 | archiveBaseName.set(jarName) |
71 | 69 | archiveClassifier.set("jar-with-dependencies") |
@@ -255,8 +253,9 @@ tasks { |
255 | 253 | include(dependency("com.squareup.okio:okio:.*")) |
256 | 254 | include(dependency("com.squareup.okio:okio-jvm:.*")) |
257 | 255 | } |
| 256 | + // transform(DebugTransformer()) |
258 | 257 | mergeServiceFiles() |
259 | | - transform(Log4j2PluginsCustomTransformer()) |
| 258 | + // transform(Log4j2PluginsCustomTransformer()) |
260 | 259 | transform(DontIncludeResourceTransformer::class.java) { |
261 | 260 | resource = "LICENSE" |
262 | 261 | } |
@@ -334,53 +333,52 @@ signing { |
334 | 333 | sign(publishing.publications["mavenJavaPkgRun"]) |
335 | 334 | } |
336 | 335 |
|
337 | | -/** |
338 | | - * Modified from the original, to simplify (and as the original was not working) |
339 | | - * |
340 | | - * Modified from [org.apache.logging.log4j.maven.plugins.shade.transformer.Log4j2PluginCacheFileTransformer.java](https://github.com/apache/logging-log4j-transform/blob/main/log4j-transform-maven-shade-plugin-extensions/src/main/java/org/apache/logging/log4j/maven/plugins/shade/transformer/Log4j2PluginCacheFileTransformer.java). |
341 | | - * |
342 | | - * @author Christopher Grote |
343 | | - * @author Paul Nelson Baker |
344 | | - * @author John Engelman |
345 | | - */ |
346 | | -@CacheableTransformer |
347 | | -open class Log4j2PluginsCustomTransformer : com.github.jengelman.gradle.plugins.shadow.transformers.Transformer { |
348 | | - private val temporaryFiles = mutableListOf<File>() |
349 | | - private var stats: ShadowStats? = null |
| 336 | +val mergeLog4jPlugins by tasks.registering { |
| 337 | + // Declare that this task needs the resolved classpath first |
| 338 | + val classpathFiles = |
| 339 | + configurations.runtimeClasspath |
| 340 | + .get() |
| 341 | + .incoming |
| 342 | + .artifactView { |
| 343 | + lenient(true) // Optional: allows build to continue even if some artifacts fail |
| 344 | + }.files |
350 | 345 |
|
351 | | - override fun canTransformResource(element: FileTreeElement): Boolean = PluginProcessor.PLUGIN_CACHE_FILE == element.name |
| 346 | + inputs.files(classpathFiles) |
352 | 347 |
|
353 | | - override fun transform(context: TransformerContext) { |
354 | | - val temporaryFile = File.createTempFile("Log4j2Plugins", ".dat") |
355 | | - temporaryFile.deleteOnExit() |
356 | | - temporaryFiles.add(temporaryFile) |
357 | | - val fos = temporaryFile.outputStream() |
358 | | - context.inputStream.use { |
359 | | - it.copyTo(fos) |
360 | | - } |
361 | | - if (stats == null) { |
362 | | - stats = context.stats |
| 348 | + doLast { |
| 349 | + val pluginCache = PluginCache() |
| 350 | + val datFiles = mutableListOf<URL>() |
| 351 | + val tempFiles = mutableListOf<File>() |
| 352 | + |
| 353 | + configurations.runtimeClasspath.get().files.forEach { file -> |
| 354 | + if (file.extension == "jar") { |
| 355 | + JarFile(file).use { jar -> |
| 356 | + val entry = jar.getEntry("META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat") |
| 357 | + if (entry != null) { |
| 358 | + println("Found Log4j2Plugins.dat in: ${file.name}") |
| 359 | + val tempFile = File.createTempFile("plugins", ".dat") |
| 360 | + jar.getInputStream(entry).use { input -> |
| 361 | + tempFile.outputStream().use { output -> |
| 362 | + input.copyTo(output) |
| 363 | + } |
| 364 | + } |
| 365 | + datFiles.add(tempFile.toURI().toURL()) |
| 366 | + tempFiles.add(tempFile) |
| 367 | + } |
| 368 | + } |
| 369 | + } |
363 | 370 | } |
364 | | - } |
365 | 371 |
|
366 | | - override fun hasTransformedResource(): Boolean = temporaryFiles.isNotEmpty() |
| 372 | + pluginCache.loadCacheFiles(Collections.enumeration(datFiles)) |
| 373 | + val outputFile = |
| 374 | + layout.buildDirectory |
| 375 | + .file("log4j-plugins/Log4j2Plugins.dat") |
| 376 | + .get() |
| 377 | + .asFile |
| 378 | + outputFile.parentFile.mkdirs() |
| 379 | + pluginCache.writeCache(outputFile.outputStream()) |
| 380 | + println("Merged ${datFiles.size} plugin cache files") |
367 | 381 |
|
368 | | - override fun modifyOutputStream( |
369 | | - os: ZipOutputStream, |
370 | | - preserveFileTimestamps: Boolean, |
371 | | - ) { |
372 | | - val pluginCache = PluginCache() |
373 | | - pluginCache.loadCacheFiles(urlEnumeration) |
374 | | - val entry = ZipEntry(PluginProcessor.PLUGIN_CACHE_FILE) |
375 | | - entry.time = getEntryTimestamp(preserveFileTimestamps, entry.time) |
376 | | - os.putNextEntry(entry) |
377 | | - pluginCache.writeCache(CloseShieldOutputStream.wrap(os)) |
378 | | - temporaryFiles.clear() |
| 382 | + tempFiles.forEach { it.delete() } |
379 | 383 | } |
380 | | - |
381 | | - private val urlEnumeration: Enumeration<URL> |
382 | | - get() { |
383 | | - val urls = temporaryFiles.map { it.toURI().toURL() } |
384 | | - return Collections.enumeration(urls) |
385 | | - } |
386 | 384 | } |
0 commit comments