|
| 1 | +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar |
| 2 | + |
| 3 | +plugins { |
| 4 | + id("otel.java-conventions") |
| 5 | + id("com.gradleup.shadow") |
| 6 | +} |
| 7 | + |
| 8 | +// this configuration collects libs that will be placed in the bootstrap classloader |
| 9 | +val bootstrapLibs 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 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 by configurations.creating { |
| 20 | + isCanBeResolved = true |
| 21 | + isCanBeConsumed = false |
| 22 | +} |
| 23 | + |
| 24 | +dependencies { |
| 25 | + bootstrapLibs(project(":bootstrap")) |
| 26 | + |
| 27 | + javaagentLibs(project(":custom")) |
| 28 | + javaagentLibs(project(":instrumentation:servlet-3")) |
| 29 | + |
| 30 | + upstreamAgent("io.opentelemetry.javaagent:opentelemetry-javaagent:$opentelemetryJavaagentVersion") |
| 31 | +} |
| 32 | + |
| 33 | +fun isolateClasses(jars: Iterable<File>): CopySpec = copySpec { |
| 34 | + jars.forEach { |
| 35 | + from(zipTree(it)) { |
| 36 | + into("inst") |
| 37 | + rename("^(.*)\\.class\$", "\$1.classdata") |
| 38 | + exclude("^LICENSE\$") |
| 39 | + exclude("META-INF/INDEX.LIST") |
| 40 | + exclude("META-INF/*.DSA") |
| 41 | + exclude("META-INF/*.SF") |
| 42 | + exclude("META-INF/maven/**") |
| 43 | + exclude("META-INF/MANIFEST.MF") |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +tasks { |
| 49 | + jar { |
| 50 | + enabled = false |
| 51 | + } |
| 52 | + |
| 53 | + // building the final javaagent jar is done in 3 steps: |
| 54 | + |
| 55 | + // 1. all distro specific javaagent libs are relocated |
| 56 | + val relocateJavaagentLibs by registering(ShadowJar::class) { |
| 57 | + configurations = listOf(javaagentLibs) |
| 58 | + |
| 59 | + archiveFileName.set("javaagentLibs-relocated.jar") |
| 60 | + |
| 61 | + duplicatesStrategy = DuplicatesStrategy.FAIL |
| 62 | + mergeServiceFiles() |
| 63 | + // mergeServiceFiles requires that duplicate strategy is set to include |
| 64 | + filesMatching("META-INF/services/**") { |
| 65 | + duplicatesStrategy = DuplicatesStrategy.INCLUDE |
| 66 | + } |
| 67 | + exclude("**/module-info.class") |
| 68 | + relocatePackages(this) |
| 69 | + |
| 70 | + // exclude known bootstrap dependencies - they can't appear in the inst/ directory |
| 71 | + dependencies { |
| 72 | + exclude(dependency("io.opentelemetry:opentelemetry-api")) |
| 73 | + exclude(dependency("io.opentelemetry:opentelemetry-common")) |
| 74 | + exclude(dependency("io.opentelemetry:opentelemetry-context")) |
| 75 | + exclude(dependency("io.opentelemetry.semconv:opentelemetry-semconv")) |
| 76 | + exclude(dependency("io.opentelemetry.semconv:opentelemetry-semconv-incubating")) |
| 77 | + // events API and metrics advice API |
| 78 | + exclude(dependency("io.opentelemetry:opentelemetry-api-incubator")) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // 2. the distro javaagent libs are then isolated - moved to the inst/ directory |
| 83 | + // having a separate task for isolating javaagent libs is required to avoid duplicates with the upstream javaagent |
| 84 | + // duplicatesStrategy in shadowJar won't be applied when adding files with with(CopySpec) because each CopySpec has |
| 85 | + // its own duplicatesStrategy |
| 86 | + val isolateJavaagentLibs by registering(Copy::class) { |
| 87 | + dependsOn(relocateJavaagentLibs) |
| 88 | + with(isolateClasses(relocateJavaagentLibs.get().outputs.files)) |
| 89 | + |
| 90 | + into(layout.buildDirectory.dir("isolated/javaagentLibs")) |
| 91 | + } |
| 92 | + |
| 93 | + // 3. the relocated and isolated javaagent libs are merged together with the bootstrap libs (which undergo relocation |
| 94 | + // in this task) and the upstream javaagent jar; duplicates are removed |
| 95 | + shadowJar { |
| 96 | + configurations = listOf(bootstrapLibs, upstreamAgent) |
| 97 | + |
| 98 | + from(isolateJavaagentLibs) |
| 99 | + |
| 100 | + archiveClassifier.set("") |
| 101 | + |
| 102 | + duplicatesStrategy = DuplicatesStrategy.EXCLUDE |
| 103 | + mergeServiceFiles("inst/META-INF/services") |
| 104 | + // mergeServiceFiles requires that duplicate strategy is set to include |
| 105 | + filesMatching("inst/META-INF/services/**") { |
| 106 | + duplicatesStrategy = DuplicatesStrategy.INCLUDE |
| 107 | + } |
| 108 | + |
| 109 | + exclude("**/module-info.class") |
| 110 | + relocatePackages(this) |
| 111 | + |
| 112 | + manifest { |
| 113 | + attributes["Main-Class"] = "io.opentelemetry.javaagent.OpenTelemetryAgent" |
| 114 | + attributes["Agent-Class"] = "io.opentelemetry.javaagent.OpenTelemetryAgent" |
| 115 | + attributes["Premain-Class"] = "io.opentelemetry.javaagent.OpenTelemetryAgent" |
| 116 | + attributes["Can-Redefine-Classes"] = "true" |
| 117 | + attributes["Can-Retransform-Classes"] = "true" |
| 118 | + attributes["Implementation-Vendor"] = "Demo" |
| 119 | + attributes["Implementation-Version"] = "demo-${project.version}-otel-$opentelemetryJavaagentVersion" |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + assemble { |
| 124 | + dependsOn(shadowJar) |
| 125 | + } |
| 126 | +} |
0 commit comments