Skip to content

Commit e226168

Browse files
authored
Merge pull request #2016 from atlanhq/dependencies
Bump shadow plugin, retaining OTel logging plugins
2 parents 813e8b8 + e9703a9 commit e226168

2 files changed

Lines changed: 52 additions & 55 deletions

File tree

gradle/libs.versions.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,5 @@ otel = [ "otel-api", "otel-sdk", "otel-exporter", "otel-autoconfig", "otel-appen
106106
poi = [ "apache-poi", "apache-poi-ooxml" ]
107107

108108
[plugins]
109-
shadow = { id = "com.gradleup.shadow", version = "9.0.0-beta5" }
109+
shadow = { id = "com.gradleup.shadow", version = "9.2.2" }
110110
git-publish = { id = "org.ajoberstar.git-publish", version = "4.2.2" }
111-
pkl = { id = "org.pkl-lang", version = "0.29.1" }

package-toolkit/runtime/build.gradle.kts

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
// 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
42
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
83
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
124
import java.net.URL
135
import java.util.Collections
14-
import java.util.Enumeration
6+
import java.util.jar.JarFile
157

168
version = providers.gradleProperty("VERSION_NAME").get()
179
val jarName = "package-toolkit-runtime"
@@ -66,6 +58,12 @@ java {
6658
tasks {
6759
shadowJar {
6860
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+
}
6967
isZip64 = true
7068
archiveBaseName.set(jarName)
7169
archiveClassifier.set("jar-with-dependencies")
@@ -255,8 +253,9 @@ tasks {
255253
include(dependency("com.squareup.okio:okio:.*"))
256254
include(dependency("com.squareup.okio:okio-jvm:.*"))
257255
}
256+
// transform(DebugTransformer())
258257
mergeServiceFiles()
259-
transform(Log4j2PluginsCustomTransformer())
258+
// transform(Log4j2PluginsCustomTransformer())
260259
transform(DontIncludeResourceTransformer::class.java) {
261260
resource = "LICENSE"
262261
}
@@ -334,53 +333,52 @@ signing {
334333
sign(publishing.publications["mavenJavaPkgRun"])
335334
}
336335

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
350345

351-
override fun canTransformResource(element: FileTreeElement): Boolean = PluginProcessor.PLUGIN_CACHE_FILE == element.name
346+
inputs.files(classpathFiles)
352347

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+
}
363370
}
364-
}
365371

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")
367381

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() }
379383
}
380-
381-
private val urlEnumeration: Enumeration<URL>
382-
get() {
383-
val urls = temporaryFiles.map { it.toURI().toURL() }
384-
return Collections.enumeration(urls)
385-
}
386384
}

0 commit comments

Comments
 (0)