@@ -9,6 +9,7 @@ plugins {
99 eclipse
1010 idea
1111
12+ id(" biz.aQute.bnd.builder" )
1213 id(" otel.errorprone-conventions" )
1314 id(" otel.jacoco-conventions" )
1415 id(" otel.spotless-conventions" )
@@ -132,6 +133,38 @@ tasks {
132133 }
133134 }
134135
136+ afterEvaluate {
137+ if (otelJava.osgiEnabled.get()) {
138+ named<Jar >(" jar" ) {
139+ // Configure OSGi metadata
140+ bundle {
141+ // Compute import packages.
142+ // Certain packages like javax.annotation.* are always optional.
143+ // Modules may have additional optional packages, typically corresponding to compileOnly dependencies.
144+ // Append wildcard "*" last to import any other referenced packages.
145+ val optionalPackages = mutableListOf (" javax.annotation" )
146+ optionalPackages.addAll(otelJava.osgiOptionalPackages.get())
147+ val importPackages = optionalPackages.joinToString(" ," ) { " $it .*;resolution:=optional;version=\" \$ {@}\" " } + " ,*"
148+
149+ // Packages not on the compile classpath (e.g. due to circular dependencies) cannot use
150+ // version="${@}" since BND cannot resolve the version. Add them as optional imports without
151+ // a version constraint; they are listed before the wildcard so BND uses our explicit
152+ // instruction rather than auto-detecting them with a version.
153+ val unversionedOptionalPackages = otelJava.osgiUnversionedOptionalPackages.get()
154+ val unversionedImports = unversionedOptionalPackages.joinToString(" ," ) { " $it .*;resolution:=optional" }
155+ val fullImportPackages = if (unversionedImports.isNotEmpty()) " $unversionedImports ,$importPackages " else importPackages
156+
157+ bnd(mapOf (
158+ // Exclude shaded internal packages from exports; they are implementation details and
159+ // should not be part of the OSGi bundle's public API surface.
160+ " -exportcontents" to " !io.opentelemetry.internal.shaded.*,io.opentelemetry.*" ,
161+ " Import-Package" to fullImportPackages
162+ ))
163+ }
164+ }
165+ }
166+ }
167+
135168 withType<Jar >().configureEach {
136169 inputs.property(" moduleName" , otelJava.moduleName)
137170
0 commit comments