build.gradle 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  2. plugins {
  3. id("com.github.johnrengelman.shadow")
  4. }
  5. apply from: "$rootDir/gradle/shadow.gradle"
  6. def relocatePackages = ext.relocatePackages
  7. configurations {
  8. // this configuration collects libs that will be placed in the bootstrap classloader
  9. bootstrapLibs {
  10. canBeResolved = true
  11. canBeConsumed = false
  12. }
  13. // this configuration collects libs that will be placed in the agent classloader, isolated from the instrumented application code
  14. javaagentLibs {
  15. canBeResolved = true
  16. canBeConsumed = false
  17. }
  18. // this configuration stores the upstream agent dep that's extended by this project
  19. upstreamAgent {
  20. canBeResolved = true
  21. canBeConsumed = false
  22. }
  23. }
  24. dependencies {
  25. bootstrapLibs(project(":bootstrap"))
  26. javaagentLibs(project(":custom"))
  27. javaagentLibs(project(":instrumentation:servlet-3"))
  28. upstreamAgent("io.opentelemetry.javaagent:opentelemetry-javaagent:${versions.opentelemetryJavaagent}")
  29. }
  30. CopySpec isolateClasses(Iterable<File> jars) {
  31. return copySpec {
  32. jars.forEach {
  33. from(zipTree(it)) {
  34. into("inst")
  35. rename("^(.*)\\.class\$", "\$1.classdata")
  36. // Rename LICENSE file since it clashes with license dir on non-case sensitive FSs (i.e. Mac)
  37. rename("^LICENSE\$", "LICENSE.renamed")
  38. exclude("META-INF/INDEX.LIST")
  39. exclude("META-INF/*.DSA")
  40. exclude("META-INF/*.SF")
  41. }
  42. }
  43. }
  44. }
  45. tasks {
  46. jar {
  47. enabled = false
  48. }
  49. // building the final javaagent jar is done in 3 steps:
  50. // 1. all distro specific javaagent libs are relocated
  51. task relocateJavaagentLibs(type: ShadowJar) {
  52. configurations = [project.configurations.javaagentLibs]
  53. duplicatesStrategy = DuplicatesStrategy.FAIL
  54. archiveFileName.set("javaagentLibs-relocated.jar")
  55. mergeServiceFiles()
  56. exclude("**/module-info.class")
  57. relocatePackages(it)
  58. // exclude known bootstrap dependencies - they can't appear in the inst/ directory
  59. dependencies {
  60. exclude("io.opentelemetry:opentelemetry-api")
  61. exclude("io.opentelemetry:opentelemetry-context")
  62. // events API and metrics advice API
  63. exclude("io.opentelemetry:opentelemetry-api-incubator")
  64. }
  65. }
  66. // 2. the distro javaagent libs are then isolated - moved to the inst/ directory
  67. // having a separate task for isolating javaagent libs is required to avoid duplicates with the upstream javaagent
  68. // duplicatesStrategy in shadowJar won't be applied when adding files with with(CopySpec) because each CopySpec has
  69. // its own duplicatesStrategy
  70. task isolateJavaagentLibs(type: Copy) {
  71. dependsOn(tasks.relocateJavaagentLibs)
  72. with isolateClasses(tasks.relocateJavaagentLibs.outputs.files)
  73. into(layout.buildDirectory.dir("isolated/javaagentLibs"))
  74. }
  75. // 3. the relocated and isolated javaagent libs are merged together with the bootstrap libs (which undergo relocation
  76. // in this task) and the upstream javaagent jar; duplicates are removed
  77. shadowJar {
  78. configurations = [project.configurations.bootstrapLibs, project.configurations.upstreamAgent]
  79. dependsOn(tasks.isolateJavaagentLibs)
  80. from(tasks.isolateJavaagentLibs.outputs)
  81. archiveClassifier.set("all")
  82. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  83. mergeServiceFiles {
  84. include("inst/META-INF/services/*")
  85. }
  86. exclude("**/module-info.class")
  87. relocatePackages(it)
  88. manifest {
  89. attributes.put("Main-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
  90. attributes.put("Agent-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
  91. attributes.put("Premain-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
  92. attributes.put("Can-Redefine-Classes", "true")
  93. attributes.put("Can-Retransform-Classes", "true")
  94. attributes.put("Implementation-Vendor", "Demo")
  95. attributes.put("Implementation-Version", "demo-${project.version}-otel-${versions.opentelemetryJavaagent}")
  96. }
  97. }
  98. assemble {
  99. dependsOn(shadowJar)
  100. }
  101. }