|
| 1 | +/** Common setup for manual instrumentation of libraries and javaagent instrumentation. */ |
| 2 | + |
| 3 | +plugins { |
| 4 | + `java-library` |
| 5 | +} |
| 6 | + |
| 7 | +/** |
| 8 | + * We define three dependency configurations to use when adding dependencies to libraries being |
| 9 | + * instrumented. |
| 10 | + * |
| 11 | + * - library: A dependency on the instrumented library. Results in the dependency being added to |
| 12 | + * compileOnly and testImplementation. If the build is run with -PtestLatestDeps=true, the |
| 13 | + * version when added to testImplementation will be overridden by `+`, the latest version |
| 14 | + * possible. For simple libraries without different behavior between versions, it is possible |
| 15 | + * to have a single dependency on library only. |
| 16 | + * |
| 17 | + * - testLibrary: A dependency on a library for testing. This will usually be used to either |
| 18 | + * a) use a different version of the library for compilation and testing and b) to add a helper |
| 19 | + * that is only required for tests (e.g., library-testing artifact). The dependency will be |
| 20 | + * added to testImplementation and will have a version of `+` when testing latest deps as |
| 21 | + * described above. |
| 22 | + * |
| 23 | + * - latestDepTestLibrary: A dependency on a library for testing when testing of latest dependency |
| 24 | + * version is enabled. This dependency will be added as-is to testImplementation, but only if |
| 25 | + * -PtestLatestDeps=true. The version will not be modified but it will be given highest |
| 26 | + * precedence. Use this to restrict the latest version dependency from the default `+`, for |
| 27 | + * example to restrict to just a major version by specifying `2.+`. |
| 28 | + */ |
| 29 | + |
| 30 | +val testLatestDeps = gradle.startParameter.projectProperties["testLatestDeps"] == "true" |
| 31 | +extra["testLatestDeps"] = testLatestDeps |
| 32 | + |
| 33 | +@CacheableRule |
| 34 | +abstract class TestLatestDepsRule : ComponentMetadataRule { |
| 35 | + override fun execute(context: ComponentMetadataContext) { |
| 36 | + val version = context.details.id.version |
| 37 | + if (version.contains("-alpha", true) |
| 38 | + || version.contains("-beta", true) |
| 39 | + || version.contains("-rc", true) |
| 40 | + || version.contains(".rc", true) |
| 41 | + || version.contains("-m", true) // e.g. spring milestones are published to grails repo |
| 42 | + || version.contains(".m", true) // e.g. lettuce |
| 43 | + || version.contains(".alpha", true) // e.g. netty |
| 44 | + || version.contains(".beta", true) // e.g. hibernate |
| 45 | + || version.contains(".cr", true) // e.g. hibernate |
| 46 | + || version.endsWith("-nf-execution") // graphql |
| 47 | + || GIT_SHA_PATTERN.matches(version) // graphql |
| 48 | + || DATETIME_PATTERN.matches(version) // graphql |
| 49 | + ) { |
| 50 | + context.details.status = "milestone" |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + companion object { |
| 55 | + private val GIT_SHA_PATTERN = Regex("^.*-[0-9a-f]{7,}$") |
| 56 | + private val DATETIME_PATTERN = Regex("^\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}.*$") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +configurations { |
| 61 | + val library by creating { |
| 62 | + isCanBeResolved = false |
| 63 | + isCanBeConsumed = false |
| 64 | + } |
| 65 | + val testLibrary by creating { |
| 66 | + isCanBeResolved = false |
| 67 | + isCanBeConsumed = false |
| 68 | + } |
| 69 | + val latestDepTestLibrary by creating { |
| 70 | + isCanBeResolved = false |
| 71 | + isCanBeConsumed = false |
| 72 | + } |
| 73 | + |
| 74 | + val testImplementation by getting |
| 75 | + |
| 76 | + listOf(library, testLibrary).forEach { configuration -> |
| 77 | + // We use whenObjectAdded and copy into the real configurations instead of extension to allow |
| 78 | + // mutating the version for latest dep tests. |
| 79 | + configuration.dependencies.whenObjectAdded { |
| 80 | + val dep = copy() |
| 81 | + if (testLatestDeps) { |
| 82 | + (dep as ExternalDependency).version { |
| 83 | + require("latest.release") |
| 84 | + } |
| 85 | + } |
| 86 | + testImplementation.dependencies.add(dep) |
| 87 | + } |
| 88 | + } |
| 89 | + if (testLatestDeps) { |
| 90 | + dependencies { |
| 91 | + components { |
| 92 | + all<TestLatestDepsRule>() |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + latestDepTestLibrary.dependencies.whenObjectAdded { |
| 97 | + val dep = copy() |
| 98 | + val declaredVersion = dep.version |
| 99 | + if (declaredVersion != null) { |
| 100 | + (dep as ExternalDependency).version { |
| 101 | + strictly(declaredVersion) |
| 102 | + } |
| 103 | + } |
| 104 | + testImplementation.dependencies.add(dep) |
| 105 | + } |
| 106 | + } |
| 107 | + named("compileOnly") { |
| 108 | + extendsFrom(library) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +if (testLatestDeps) { |
| 113 | + afterEvaluate { |
| 114 | + tasks { |
| 115 | + withType<JavaCompile>().configureEach { |
| 116 | + with(options) { |
| 117 | + // We may use methods that are deprecated in future versions, we check lint on the normal |
| 118 | + // build and don't need this for testLatestDeps. |
| 119 | + compilerArgs.add("-Xlint:-deprecation") |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (tasks.names.contains("latestDepTest")) { |
| 125 | + val latestDepTest by tasks.existing |
| 126 | + tasks.named("test").configure { |
| 127 | + dependsOn(latestDepTest) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} else { |
| 132 | + afterEvaluate { |
| 133 | + // Disable compiling latest dep tests for non latest dep builds in CI. This is needed to avoid |
| 134 | + // breaking build because of a new library version which could force backporting latest dep |
| 135 | + // fixes to release branches. |
| 136 | + // This is only needed for modules where base version and latest dep tests use a different |
| 137 | + // source directory. |
| 138 | + var latestDepCompileTaskNames = arrayOf("compileLatestDepTestJava", "compileLatestDepTestGroovy", "compileLatestDepTestScala") |
| 139 | + for (compileTaskName in latestDepCompileTaskNames) { |
| 140 | + if (tasks.names.contains(compileTaskName)) { |
| 141 | + tasks.named(compileTaskName).configure { |
| 142 | + enabled = false |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +tasks { |
| 150 | + val generateInstrumentationVersionFile by registering { |
| 151 | + val name = computeInstrumentationName() |
| 152 | + val version = project.version as String |
| 153 | + inputs.property("instrumentation.name", name) |
| 154 | + inputs.property("instrumentation.version", version) |
| 155 | + |
| 156 | + val propertiesDir = layout.buildDirectory.dir("generated/instrumentationVersion/META-INF/io/opentelemetry/instrumentation/") |
| 157 | + outputs.dir(propertiesDir) |
| 158 | + |
| 159 | + doLast { |
| 160 | + File(propertiesDir.get().asFile, "$name.properties").writeText("version=$version") |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +fun computeInstrumentationName(): String { |
| 166 | + val name = when (projectDir.name) { |
| 167 | + "javaagent", "library", "library-autoconfigure" -> projectDir.parentFile.name |
| 168 | + else -> project.name |
| 169 | + } |
| 170 | + return "io.opentelemetry.$name" |
| 171 | +} |
| 172 | + |
| 173 | +sourceSets { |
| 174 | + main { |
| 175 | + output.dir("build/generated/instrumentationVersion", "builtBy" to "generateInstrumentationVersionFile") |
| 176 | + } |
| 177 | +} |
0 commit comments