@@ -394,6 +394,46 @@ jar {
394394 -exportcontents: io.micrometer.*
395395 ''' . stripIndent()
396396 }
397+
398+ // Remove sun.misc from versioned OSGi manifests (Java 11+).
399+ // Java 11+ uses StackWalker instead of sun.misc, so the import is unnecessary.
400+ // bnd generates versioned manifests automatically via JPMSMultiReleasePlugin but
401+ // has no per-version Import-Package control, so we post-process the JAR.
402+ doLast {
403+ def jarFile = archiveFile. get(). asFile
404+ def jarUri = URI . create(" jar:${ jarFile.toURI()} " )
405+ def env = [create : ' false' ]
406+
407+ java.nio.file.FileSystems . newFileSystem(jarUri, env). withCloseable { fs ->
408+ def versionsDir = fs. getPath(' META-INF/versions' )
409+ if (! java.nio.file.Files . exists(versionsDir)) return
410+
411+ java.nio.file.Files . list(versionsDir). withCloseable { versions ->
412+ versions. each { versionDir ->
413+ def manifestPath = versionDir. resolve(' OSGI-INF/MANIFEST.MF' )
414+ if (! java.nio.file.Files . exists(manifestPath)) return
415+
416+ def manifest = new java.util.jar.Manifest ()
417+ manifestPath. newInputStream(). withCloseable { manifest. read(it) }
418+
419+ def importPkg = manifest. mainAttributes. getValue(' Import-Package' )
420+ if (importPkg == null || ! importPkg. contains(' sun.misc' )) return
421+
422+ // Remove sun.misc with optional directives (e.g. ;version="...";resolution:=optional)
423+ def modified = importPkg
424+ .replaceAll(' ,sun\\ .misc(;[^,]*)?' , ' ' )
425+ .replaceAll(' sun\\ .misc(;[^,]*)?,' , ' ' )
426+ .replaceAll(' sun\\ .misc(;[^,]*)?' , ' ' )
427+ .replaceAll(' ,\\ s*$' , ' ' )
428+
429+ manifest. mainAttributes. putValue(' Import-Package' , modified)
430+ manifestPath. newOutputStream(). withCloseable { manifest. write(it) }
431+
432+ logger. lifecycle(" Removed sun.misc from versioned manifest: ${ manifestPath} " )
433+ }
434+ }
435+ }
436+ }
397437}
398438
399439jacocoTestReport. dependsOn test
0 commit comments