@@ -308,26 +308,30 @@ shadowJar {
308308 }
309309}
310310
311- // Apply bnd to shadowJar task manually for Gradle 9 compatibility
311+ // Apply bnd to shadowJar task manually for Gradle 9 compatibility.
312+ // The bnd builder plugin only adds a BundleTaskExtension to the 'jar' task, so
313+ // the extension must be created here and its build action registered
314+ // explicitly; otherwise the published jar has no OSGi headers.
312315tasks. named(' shadowJar' ). configure {
313- // Get the BundleTaskExtension added by bnd plugin
314- def bundle = extensions. findByType(BundleTaskExtension )
315- if (bundle != null ) {
316- // Configure bnd for shadowJar
317- // -exportcontents: graphql.* Adds all packages of graphql and below to the exported packages list
318- // -removeheaders: Private-Package Removes the MANIFEST.MF header Private-Package, which contains all the internal packages and
319- // also the repackaged packages like guava, which would be wrong after repackaging.
320- // Import-Package: Changes the imported packages header, to exclude guava and dependencies from the import list (! excludes packages)
321- // Guava was repackaged and included inside the jar, so we need to remove it.
322- // ANTLR was shaded, so we need to remove it.
323- // sun.misc is a JRE internal-only class that is not directly used by graphql-java. It was causing problems in libraries using graphql-java.
324- // The last ,* copies all the existing imports from the other dependencies, which is required.
325- bundle. bnd('''
316+ def bundle = extensions. create(BundleTaskExtension . NAME , BundleTaskExtension , it)
317+ // Configure bnd for shadowJar
318+ // Bundle-SymbolicName / Bundle-Name: bnd defaults them to the archive name, which includes the 'shadow' classifier.
319+ // -exportcontents: graphql.* Adds all packages of graphql and below to the exported packages list
320+ // -removeheaders: Private-Package Removes the MANIFEST.MF header Private-Package, which contains all the internal packages and
321+ // also the repackaged packages like guava, which would be wrong after repackaging.
322+ // Import-Package: Changes the imported packages header, to exclude guava and dependencies from the import list (! excludes packages)
323+ // Guava was repackaged and included inside the jar, so we need to remove it.
324+ // ANTLR was shaded, so we need to remove it.
325+ // sun.misc is a JRE internal-only class that is not directly used by graphql-java. It was causing problems in libraries using graphql-java.
326+ // The last ,* copies all the existing imports from the other dependencies, which is required.
327+ bundle. bnd('''
328+ Bundle-SymbolicName: graphql-java
329+ Bundle-Name: graphql-java
326330-exportcontents: graphql.*
327331-removeheaders: Private-Package
328332Import-Package: !android.os.*,!com.google.*,!org.checkerframework.*,!graphql.com.google.*,!org.antlr.*,!graphql.org.antlr.*,!sun.misc.*,org.jspecify.annotations;resolution:=optional,*
329333''' )
330- }
334+ doLast(bundle . buildAction())
331335}
332336
333337tasks. named(' jmhJar' ) {
@@ -587,7 +591,46 @@ def compileShadedJarConsumer = tasks.register('compileShadedJarConsumer', JavaCo
587591 options. compilerArgs. addAll([' -Xlint:classfile' , ' -Werror' ])
588592}
589593
590- check. dependsOn verifyShadedClassAnnotations, compileShadedJarConsumer
594+ /**
595+ * Verifies that the published jar is a valid OSGi bundle: it must carry the
596+ * bnd-generated headers for graphql-java, export the graphql packages and must
597+ * not import the shaded Guava and ANTLR packages.
598+ */
599+ def verifyOsgiManifest = tasks. register(' verifyOsgiManifest' ) {
600+ group = ' verification'
601+ description = ' Verify the published jar has the expected OSGi manifest headers'
602+ dependsOn buildFinalJar
603+
604+ def finalJar = buildFinalJar. flatMap { it. archiveFile }
605+ inputs. file(finalJar)
606+
607+ doLast {
608+ def attributes = new java.util.jar.JarFile (finalJar. get(). asFile). withCloseable { it. manifest. mainAttributes }
609+ def errors = []
610+ if (attributes. getValue(' Bundle-SymbolicName' ) != ' graphql-java' ) {
611+ errors. add(" Bundle-SymbolicName is '${ attributes.getValue('Bundle-SymbolicName')} ', expected 'graphql-java'" )
612+ }
613+ if (! attributes. getValue(' Bundle-Version' )) {
614+ errors. add(' Bundle-Version is missing' )
615+ }
616+ def exportPackage = attributes. getValue(' Export-Package' ) ?: ' '
617+ if (! (exportPackage =~ / (^|,)graphql;/ )) {
618+ errors. add(' Export-Package does not export the graphql package' )
619+ }
620+ if (attributes. getValue(' Private-Package' ) != null ) {
621+ errors. add(' Private-Package header should have been removed' )
622+ }
623+ def importPackage = attributes. getValue(' Import-Package' ) ?: ' '
624+ if (importPackage =~ / (^|,)(com\. google|org\. antlr)\. / ) {
625+ errors. add(" Import-Package references shaded packages: ${ importPackage} " )
626+ }
627+ if (! errors. isEmpty()) {
628+ throw new GradleException (" Invalid OSGi manifest in ${ finalJar.get().asFile.name} :\n ${ errors.join('\n')} " )
629+ }
630+ }
631+ }
632+
633+ check. dependsOn verifyShadedClassAnnotations, compileShadedJarConsumer, verifyOsgiManifest
591634
592635
593636// --- TestNG TCK skip verification ---
0 commit comments