build.gradle.kts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  2. import com.github.jk1.license.filter.LicenseBundleNormalizer
  3. import com.github.jk1.license.render.InventoryMarkdownReportRenderer
  4. plugins {
  5. id("com.github.jk1.dependency-license-report")
  6. id("otel.java-conventions")
  7. id("otel.publish-conventions")
  8. id("io.opentelemetry.instrumentation.javaagent-shadowing")
  9. }
  10. description = "OpenTelemetry Javaagent"
  11. group = "io.opentelemetry.javaagent"
  12. val bootstrapLibs by configurations.creating {
  13. isCanBeResolved = true
  14. isCanBeConsumed = false
  15. }
  16. val licenseReportDependencies by configurations.creating {
  17. extendsFrom(bootstrapLibs)
  18. }
  19. dependencies {
  20. bootstrapLibs(project(":instrumentation-api"))
  21. bootstrapLibs(project(":instrumentation-api-annotation-support"))
  22. bootstrapLibs(project(":javaagent-bootstrap"))
  23. bootstrapLibs(project(":javaagent-instrumentation-api"))
  24. bootstrapLibs("org.slf4j:slf4j-simple")
  25. // We only have compileOnly dependencies on these to make sure they don"t leak into POMs.
  26. licenseReportDependencies("com.github.ben-manes.caffeine:caffeine") {
  27. isTransitive = false
  28. }
  29. licenseReportDependencies("com.blogspot.mydailyjava:weak-lock-free")
  30. // TODO ideally this would be :instrumentation instead of :javaagent-tooling
  31. // in case there are dependencies (accidentally) pulled in by instrumentation modules
  32. // but I couldn"t get that to work
  33. licenseReportDependencies(project(":javaagent-tooling"))
  34. licenseReportDependencies(project(":javaagent-extension-api"))
  35. testCompileOnly(project(":javaagent-bootstrap"))
  36. testCompileOnly(project(":javaagent-instrumentation-api"))
  37. testImplementation("com.google.guava:guava")
  38. testImplementation("io.opentracing.contrib.dropwizard:dropwizard-opentracing:0.2.2")
  39. }
  40. val javaagentDependencies = dependencies
  41. // collect all bootstrap instrumentation dependencies
  42. project(":instrumentation").subprojects {
  43. val subProj = this
  44. plugins.withId("java") {
  45. if (subProj.name == "bootstrap") {
  46. javaagentDependencies.run {
  47. add(bootstrapLibs.name, project(subProj.path))
  48. }
  49. }
  50. }
  51. }
  52. fun isolateSpec(projectsWithShadowJar: Collection<Project>): CopySpec = copySpec {
  53. from(projectsWithShadowJar.map { zipTree(it.tasks.getByName<ShadowJar>("shadowJar").archiveFile) }) {
  54. // important to keep prefix "inst" short, as it is prefixed to lots of strings in runtime mem
  55. into("inst")
  56. rename("""(^.*)\.class$""", "$1.classdata")
  57. // Rename LICENSE file since it clashes with license dir on non-case sensitive FSs (i.e. Mac)
  58. rename("""^LICENSE$""", "LICENSE.renamed")
  59. }
  60. }
  61. tasks {
  62. processResources {
  63. from(rootProject.file("licenses")) {
  64. into("META-INF/licenses")
  65. }
  66. }
  67. //Includes everything needed for OOTB experience
  68. val shadowJar by existing(ShadowJar::class) {
  69. archiveClassifier.set("all")
  70. val projectsWithShadowJar = listOf(project(":instrumentation"), project(":javaagent-exporters"))
  71. projectsWithShadowJar.forEach {
  72. dependsOn("${it.path}:shadowJar")
  73. }
  74. with(isolateSpec(projectsWithShadowJar))
  75. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  76. }
  77. //Includes instrumentations, but not exporters
  78. val lightShadow by registering(ShadowJar::class) {
  79. archiveClassifier.set("")
  80. dependsOn(":instrumentation:shadowJar")
  81. val projectsWithShadowJar = listOf(project(":instrumentation"))
  82. with(isolateSpec(projectsWithShadowJar))
  83. }
  84. withType<ShadowJar>().configureEach {
  85. configurations = listOf(bootstrapLibs)
  86. manifest {
  87. attributes(
  88. "Main-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  89. "Agent-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  90. "Premain-Class" to "io.opentelemetry.javaagent.OpenTelemetryAgent",
  91. "Can-Redefine-Classes" to true,
  92. "Can-Retransform-Classes" to true
  93. )
  94. }
  95. }
  96. // lightShadow is the default classifier we publish so disable the default jar.
  97. jar {
  98. enabled = false
  99. }
  100. withType<Test>().configureEach {
  101. inputs.file(shadowJar.get().archiveFile)
  102. jvmArgs("-Dotel.javaagent.debug=true")
  103. doFirst {
  104. // Defining here to allow jacoco to be first on the command line.
  105. jvmArgs("-javaagent:${shadowJar.get().archivePath}")
  106. }
  107. testLogging {
  108. events("started")
  109. }
  110. dependsOn(shadowJar)
  111. }
  112. named("assemble") {
  113. dependsOn(lightShadow)
  114. dependsOn(shadowJar)
  115. }
  116. val cleanLicenses by registering(Delete::class) {
  117. delete(rootProject.file("licenses"))
  118. }
  119. named("generateLicenseReport").configure {
  120. dependsOn(cleanLicenses)
  121. }
  122. publishing {
  123. publications {
  124. named<MavenPublication>("maven") {
  125. artifact(lightShadow)
  126. }
  127. }
  128. }
  129. }
  130. licenseReport {
  131. outputDir = rootProject.file("licenses").absolutePath
  132. renderers = arrayOf(InventoryMarkdownReportRenderer())
  133. configurations = arrayOf(licenseReportDependencies.name)
  134. excludeGroups = arrayOf(
  135. "io.opentelemetry.instrumentation",
  136. "io.opentelemetry.javaagent"
  137. )
  138. filters = arrayOf(LicenseBundleNormalizer("$projectDir/license-normalizer-bundle.json", true))
  139. }