|
| 1 | +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar |
| 2 | + |
| 3 | +plugins { |
| 4 | + id("ai.java-conventions") |
| 5 | + id("ai.shadow-conventions") |
| 6 | +} |
| 7 | + |
| 8 | +// this configuration collects libs that will be placed in the bootstrap classloader |
| 9 | +val bootstrapLibs: Configuration by configurations.creating { |
| 10 | + isCanBeResolved = true |
| 11 | + isCanBeConsumed = false |
| 12 | +} |
| 13 | +// this configuration collects libs that will be placed in the agent classloader, isolated from the instrumented application code |
| 14 | +val javaagentLibs: Configuration by configurations.creating { |
| 15 | + isCanBeResolved = true |
| 16 | + isCanBeConsumed = false |
| 17 | +} |
| 18 | +// this configuration stores the upstream agent dep that's extended by this project |
| 19 | +val upstreamAgent: Configuration by configurations.creating { |
| 20 | + isCanBeResolved = true |
| 21 | + isCanBeConsumed = false |
| 22 | +} |
| 23 | + |
| 24 | +dependencies { |
| 25 | + // include everything from otel agent for testing |
| 26 | + upstreamAgent("io.opentelemetry.javaagent:opentelemetry-agent-for-testing") |
| 27 | +} |
| 28 | + |
| 29 | +tasks { |
| 30 | + jar { |
| 31 | + enabled = false |
| 32 | + } |
| 33 | + |
| 34 | + // building the final javaagent jar is done in 3 steps: |
| 35 | + |
| 36 | + // 1. all distro-specific javaagent libs are relocated (by the ai.shadow-conventions plugin) |
| 37 | + val relocateJavaagentLibs by registering(ShadowJar::class) { |
| 38 | + configurations = listOf(javaagentLibs) |
| 39 | + |
| 40 | + archiveFileName.set("javaagentLibs-relocated.jar") |
| 41 | + } |
| 42 | + |
| 43 | + // 2. the distro javaagent libs are then isolated - moved to the inst/ directory |
| 44 | + // having a separate task for isolating javaagent libs is required to avoid duplicates with the upstream javaagent |
| 45 | + // duplicatesStrategy in shadowJar won't be applied when adding files with with(CopySpec) because each CopySpec has |
| 46 | + // its own duplicatesStrategy |
| 47 | + val isolateJavaagentLibs by registering(Copy::class) { |
| 48 | + dependsOn(relocateJavaagentLibs) |
| 49 | + isolateClasses(relocateJavaagentLibs.get().outputs.files) |
| 50 | + |
| 51 | + into("$buildDir/isolated/javaagentLibs") |
| 52 | + } |
| 53 | + |
| 54 | + // 3. the relocated and isolated javaagent libs are merged together with the bootstrap libs (which undergo relocation |
| 55 | + // in this task) and the upstream javaagent jar; duplicates are removed |
| 56 | + shadowJar { |
| 57 | + configurations = listOf(bootstrapLibs, upstreamAgent) |
| 58 | + |
| 59 | + dependsOn(isolateJavaagentLibs) |
| 60 | + from(isolateJavaagentLibs.get().outputs) |
| 61 | + |
| 62 | + duplicatesStrategy = DuplicatesStrategy.EXCLUDE |
| 63 | + |
| 64 | + manifest { |
| 65 | + attributes( |
| 66 | + mapOf( |
| 67 | + "Main-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent", |
| 68 | + "Agent-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent", |
| 69 | + "Premain-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent", |
| 70 | + "Can-Redefine-Classes" to true, |
| 71 | + "Can-Retransform-Classes" to true |
| 72 | + ) |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + assemble { |
| 78 | + dependsOn(shadowJar) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fun CopySpec.isolateClasses(jars: Iterable<File>) { |
| 83 | + jars.forEach { |
| 84 | + from(zipTree(it)) { |
| 85 | + into("inst") |
| 86 | + rename("^(.*)\\.class\$", "\$1.classdata") |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments