build.gradle 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // and finally include everything from otel agent for testing
  27. upstreamAgent("io.opentelemetry.javaagent:opentelemetry-agent-for-testing:${versions.opentelemetryJavaagentAlpha}")
  28. }
  29. CopySpec isolateClasses(Iterable<File> jars) {
  30. return copySpec {
  31. jars.forEach {
  32. from(zipTree(it)) {
  33. into("inst")
  34. rename("^(.*)\\.class\$", "\$1.classdata")
  35. // Rename LICENSE file since it clashes with license dir on non-case sensitive FSs (i.e. Mac)
  36. rename("^LICENSE\$", "LICENSE.renamed")
  37. exclude("META-INF/INDEX.LIST")
  38. exclude("META-INF/*.DSA")
  39. exclude("META-INF/*.SF")
  40. }
  41. }
  42. }
  43. }
  44. tasks {
  45. jar {
  46. enabled = false
  47. }
  48. // building the final javaagent jar is done in 3 steps:
  49. // 1. all distro specific javaagent libs are relocated
  50. task relocateJavaagentLibs(type: ShadowJar) {
  51. configurations = [project.configurations.javaagentLibs]
  52. duplicatesStrategy = DuplicatesStrategy.FAIL
  53. archiveFileName.set("javaagentLibs-relocated.jar")
  54. mergeServiceFiles()
  55. exclude("**/module-info.class")
  56. relocatePackages(it)
  57. // exclude known bootstrap dependencies - they can't appear in the inst/ directory
  58. dependencies {
  59. exclude("io.opentelemetry:opentelemetry-api")
  60. exclude("io.opentelemetry:opentelemetry-api-events")
  61. exclude("io.opentelemetry:opentelemetry-context")
  62. exclude("io.opentelemetry:opentelemetry-semconv")
  63. // metrics advice API
  64. exclude("io.opentelemetry:opentelemetry-extension-incubator")
  65. }
  66. }
  67. // 2. the distro javaagent libs are then isolated - moved to the inst/ directory
  68. // having a separate task for isolating javaagent libs is required to avoid duplicates with the upstream javaagent
  69. // duplicatesStrategy in shadowJar won't be applied when adding files with with(CopySpec) because each CopySpec has
  70. // its own duplicatesStrategy
  71. task isolateJavaagentLibs(type: Copy) {
  72. dependsOn(tasks.relocateJavaagentLibs)
  73. with isolateClasses(tasks.relocateJavaagentLibs.outputs.files)
  74. into("$buildDir/isolated/javaagentLibs")
  75. }
  76. // 3. the relocated and isolated javaagent libs are merged together with the bootstrap libs (which undergo relocation
  77. // in this task) and the upstream javaagent jar; duplicates are removed
  78. shadowJar {
  79. configurations = [project.configurations.bootstrapLibs, project.configurations.upstreamAgent]
  80. dependsOn(tasks.isolateJavaagentLibs)
  81. from(tasks.isolateJavaagentLibs.outputs)
  82. archiveClassifier.set("")
  83. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  84. mergeServiceFiles {
  85. include("inst/META-INF/services/*")
  86. }
  87. exclude("**/module-info.class")
  88. relocatePackages(it)
  89. manifest {
  90. attributes.put("Main-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
  91. attributes.put("Agent-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
  92. attributes.put("Premain-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
  93. attributes.put("Can-Redefine-Classes", "true")
  94. attributes.put("Can-Retransform-Classes", "true")
  95. attributes.put("Implementation-Vendor", "Demo")
  96. attributes.put("Implementation-Version", "demo-${project.version}-otel-${versions.opentelemetryJavaagent}")
  97. }
  98. }
  99. assemble {
  100. dependsOn(shadowJar)
  101. }
  102. }