Skip to content

Commit 3071f5c

Browse files
committed
Define optionalCompileOnly configuration
1 parent cd1f617 commit 3071f5c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ val dependencyManagement by configurations.creating {
208208
isVisible = false
209209
}
210210

211+
// An optionalCompileOnly dependency is the same as a compileOnly dependency, except that it appears
212+
// in published pom.xml like the following, where compileOnly dependencies don't appear in pom.xml at all:
213+
// <dependency>
214+
// <groupId>io.opentelemetry</groupId>
215+
// <artifactId>opentelemetry-api-incubator</artifactId>
216+
// <version>1.48.0-alpha-SNAPSHOT</version>
217+
// <scope>runtime</scope>
218+
// <optional>true</optional>
219+
// </dependency>
220+
val optionalCompileOnly by configurations.creating {
221+
isCanBeConsumed = false
222+
isCanBeResolved = false
223+
// In otel.publish-conventions.gradle.kts we indicate that optionalCompileOnly dependencies should
224+
// be added to the publication with scope=runtime, optional=true. For some reason, this fails if
225+
// the configuration doesn't have at least 1 attribute associated with it. It's not clear from the
226+
// docs (https://docs.gradle.org/current/userguide/variant_attributes.html) what the attribute is used for.
227+
attributes {
228+
attribute(Attribute.of("unused", String::class.java), "unused")
229+
}
230+
}
231+
configurations.named("compileOnly") {
232+
extendsFrom(optionalCompileOnly)
233+
}
234+
211235
dependencies {
212236
dependencyManagement(platform(project(":dependencyManagement")))
213237
afterEvaluate {

buildSrc/src/main/kotlin/otel.publish-conventions.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ publishing {
2020
}
2121
plugins.withId("java-library") {
2222
from(components["java"])
23+
24+
val javaComponent = components.findByName("java") as AdhocComponentWithVariants
25+
javaComponent.addVariantsFromConfiguration(configurations["optionalCompileOnly"]) {
26+
mapToMavenScope("runtime")
27+
mapToOptional()
28+
}
2329
}
2430

2531
versionMapping {

sdk/trace/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies {
2222
api(project(":api:all"))
2323
api(project(":sdk:common"))
2424

25-
compileOnly(project(":api:incubator"))
25+
optionalCompileOnly(project(":api:incubator"))
2626
compileOnly(project(":sdk:trace-shaded-deps"))
2727

2828
annotationProcessor("com.google.auto.value:auto-value")

0 commit comments

Comments
 (0)