Skip to content

Commit e239a0e

Browse files
committed
Use Import-Package without version instead of DynamicImport-Package
1 parent 202e349 commit e239a0e

3 files changed

Lines changed: 19 additions & 24 deletions

File tree

api/all/build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ plugins {
99
description = "OpenTelemetry API"
1010
otelJava.moduleName.set("io.opentelemetry.api")
1111
base.archivesName.set("opentelemetry-api")
12-
// These packages are accessed via Class.forName and cannot be added as compileOnly dependencies
13-
// (api:incubator depends on api:all, creating a circular dependency). Use DynamicImport-Package
14-
// so OSGi does not require them at resolution time but can wire them at runtime when available.
15-
otelJava.osgiDynamicImportPackages.set(listOf("io.opentelemetry.sdk.autoconfigure", "io.opentelemetry.api.incubator", "io.opentelemetry.api.incubator.internal"))
12+
// These packages cannot be compileOnly dependencies (api:incubator depends on api:all, creating a
13+
// circular dependency; sdk:autoconfigure is in a different module family). Declare them as optional
14+
// imports without a version constraint so OSGi wires them if present but does not require them.
15+
otelJava.osgiUnversionedOptionalPackages.set(listOf("io.opentelemetry.sdk.autoconfigure", "io.opentelemetry.api.incubator", "io.opentelemetry.api.incubator.internal"))
1616

1717
dependencies {
1818
api(project(":context"))

buildSrc/src/main/kotlin/io/opentelemetry/gradle/OtelJavaExtension.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ abstract class OtelJavaExtension {
1818

1919
abstract val osgiOptionalPackages: ListProperty<String>
2020

21-
// Packages accessed via Class.forName that are not on the compile classpath (e.g. due to
22-
// circular dependencies). These are excluded from Import-Package and added to
23-
// DynamicImport-Package so OSGi does not require them at resolution time, but can still wire
24-
// them at runtime when available.
25-
abstract val osgiDynamicImportPackages: ListProperty<String>
21+
// Packages that should be optional imports but are not on the compile classpath (e.g. due to
22+
// circular dependencies), so BND cannot resolve version="${@}" for them. Added to Import-Package
23+
// with resolution:=optional but no version constraint.
24+
abstract val osgiUnversionedOptionalPackages: ListProperty<String>
2625

2726
abstract val minJavaVersionSupported: Property<JavaVersion>
2827

2928
init {
3029
minJavaVersionSupported.convention(JavaVersion.VERSION_1_8)
3130
osgiEnabled.convention(true)
3231
osgiOptionalPackages.convention(emptyList<String>())
33-
osgiDynamicImportPackages.convention(emptyList<String>())
32+
osgiUnversionedOptionalPackages.convention(emptyList<String>())
3433
}
3534
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,20 @@ tasks {
145145
optionalPackages.addAll(otelJava.osgiOptionalPackages.get())
146146
val importPackages = optionalPackages.joinToString(",") { "$it.*;resolution:=optional;version=\"\${@}\"" } + ",*"
147147

148-
// Packages accessed via Class.forName that are not on the compile classpath cannot be made
149-
// optional via Import-Package (BND cannot resolve version="${@}" for them). Instead, exclude
150-
// them from Import-Package and declare them as DynamicImport-Package so OSGi does not
151-
// require them at resolution time but can still wire them at runtime when available.
152-
val dynamicImportPackages = otelJava.osgiDynamicImportPackages.get()
153-
val negations = dynamicImportPackages.joinToString(",") { "!$it" }
154-
val fullImportPackages = if (negations.isNotEmpty()) "$negations,$importPackages" else importPackages
155-
156-
val bndInstructions = mutableMapOf(
148+
// Packages not on the compile classpath (e.g. due to circular dependencies) cannot use
149+
// version="${@}" since BND cannot resolve the version. Add them as optional imports without
150+
// a version constraint; they are listed before the wildcard so BND uses our explicit
151+
// instruction rather than auto-detecting them with a version.
152+
val unversionedOptionalPackages = otelJava.osgiUnversionedOptionalPackages.get()
153+
val unversionedImports = unversionedOptionalPackages.joinToString(",") { "$it.*;resolution:=optional" }
154+
val fullImportPackages = if (unversionedImports.isNotEmpty()) "$unversionedImports,$importPackages" else importPackages
155+
156+
bnd(mapOf(
157157
// Exclude shaded internal packages from exports; they are implementation details and
158158
// should not be part of the OSGi bundle's public API surface.
159159
"-exportcontents" to "!io.opentelemetry.internal.shaded.*,io.opentelemetry.*",
160160
"Import-Package" to fullImportPackages
161-
)
162-
if (dynamicImportPackages.isNotEmpty()) {
163-
bndInstructions["DynamicImport-Package"] = dynamicImportPackages.joinToString(",") { "$it.*" }
164-
}
165-
bnd(bndInstructions)
161+
))
166162
}
167163
}
168164
}

0 commit comments

Comments
 (0)