Skip to content

Commit

Permalink
Remove duplicate byte buddy classes (#12571)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Nov 6, 2024
1 parent 2b5c4f5 commit 78b3c6a
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,43 @@ tasks {
archiveFileName.set("bootstrapLibs.jar")
}

val relocateBaseJavaagentLibs by registering(ShadowJar::class) {
val relocateBaseJavaagentLibsTmp by registering(ShadowJar::class) {
configurations = listOf(baseJavaagentLibs)

excludeBootstrapClasses()

duplicatesStrategy = DuplicatesStrategy.FAIL

archiveFileName.set("baseJavaagentLibs-relocated-tmp.jar")
}

val relocateBaseJavaagentLibs by registering(Jar::class) {
dependsOn(relocateBaseJavaagentLibsTmp)

copyByteBuddy(relocateBaseJavaagentLibsTmp.get().archiveFile)

duplicatesStrategy = DuplicatesStrategy.FAIL

archiveFileName.set("baseJavaagentLibs-relocated.jar")
}

val relocateJavaagentLibs by registering(ShadowJar::class) {
val relocateJavaagentLibsTmp by registering(ShadowJar::class) {
configurations = listOf(javaagentLibs)

excludeBootstrapClasses()

duplicatesStrategy = DuplicatesStrategy.FAIL

archiveFileName.set("javaagentLibs-relocated-tmp.jar")
}

val relocateJavaagentLibs by registering(Jar::class) {
dependsOn(relocateJavaagentLibsTmp)

copyByteBuddy(relocateJavaagentLibsTmp.get().archiveFile)

duplicatesStrategy = DuplicatesStrategy.FAIL

archiveFileName.set("javaagentLibs-relocated.jar")
}

Expand Down Expand Up @@ -363,6 +383,24 @@ fun CopySpec.isolateClasses(jar: Provider<RegularFile>) {
}
}

fun CopySpec.copyByteBuddy(jar: Provider<RegularFile>) {
// Byte buddy jar includes classes compiled for java 5 at the root of the jar and the same classes
// compiled for java 8 under META-INF/versions/9. Here we move the classes from
// META-INF/versions/9/net/bytebuddy to net/bytebuddy to get rid of the duplicate classes.
from(zipTree(jar)) {
eachFile {
if (path.startsWith("net/bytebuddy/") &&
// this is our class that we have placed in the byte buddy package, need to preserve it
!path.startsWith("net/bytebuddy/agent/builder/AgentBuilderUtil")) {
exclude()
} else if (path.startsWith("META-INF/versions/9/net/bytebuddy/")) {
path = path.removePrefix("META-INF/versions/9/")
}
}
includeEmptyDirs = false
}
}

// exclude bootstrap projects from javaagent libs - they won't be added to inst/
fun ShadowJar.excludeBootstrapClasses() {
dependencies {
Expand Down

0 comments on commit 78b3c6a

Please sign in to comment.