| 1 |
import aQute.bnd.gradle.BundleTaskExtension |
| 2 |
import aQute.bnd.gradle.Resolve |
| 3 |
|
| 4 |
plugins { |
| 5 |
`java-library` |
| 6 |
} |
| 7 |
|
| 8 |
val importAPIGuardian = "org.apiguardian.*;resolution:=\"optional\"" |
| 9 |
|
| 10 |
val projectDescription = objects.property<String>().convention(provider { project.description }) |
| 11 |
|
| 12 |
// This task enhances `jar` and `shadowJar` tasks with the bnd |
| 13 |
// `BundleTaskExtension` extension which allows for generating OSGi |
| 14 |
// metadata into the jar |
| 15 |
tasks.withType<Jar>().matching { task: Jar -> |
| 16 |
task.name == "jar" || task.name == "shadowJar" |
| 17 |
}.all { // configure tasks eagerly as workaround for https://github.com/bndtools/bnd/issues/5695 |
| 18 |
extra["importAPIGuardian"] = importAPIGuardian |
| 19 |
|
| 20 |
extensions.create<BundleTaskExtension>(BundleTaskExtension.NAME, this).apply { |
| 21 |
properties.set(projectDescription.map { |
| 22 |
mapOf("project.description" to it) |
| 23 |
}) |
| 24 |
// These are bnd instructions necessary for generating OSGi metadata. |
| 25 |
// We've generalized these so that they are widely applicable limiting |
| 26 |
// module configurations to special cases. |
| 27 |
setBnd( |
| 28 |
""" |
| 29 |
# Set the Bundle-SymbolicName to the archiveBaseName. |
| 30 |
# We don't use the archiveClassifier which Bnd will use |
| 31 |
# in the default Bundle-SymbolicName value. |
| 32 |
Bundle-SymbolicName: ${'$'}{task.archiveBaseName} |
| 33 |
|
| 34 |
# Set the Bundle-Name from the project description |
| 35 |
Bundle-Name: ${'$'}{project.description} |
| 36 |
|
| 37 |
# These are the general rules for package imports. |
| 38 |
Import-Package: \ |
| 39 |
${importAPIGuardian},\ |
| 40 |
org.junit.platform.commons.logging;status=INTERNAL,\ |
| 41 |
kotlin.*;resolution:="optional",\ |
| 42 |
* |
| 43 |
|
| 44 |
# This tells bnd not to complain if a module doesn't actually import |
| 45 |
# the kotlin and apiguardian packages, but enough modules do to make it a default. |
| 46 |
-fixupmessages.kotlin.import: "Unused Import-Package instructions: \\[kotlin.*\\]";is:=ignore |
| 47 |
-fixupmessages.apiguardian.import: "Unused Import-Package instructions: \\[org.apiguardian.*\\]";is:=ignore |
| 48 |
|
| 49 |
# This tells bnd to ignore classes it finds in `META-INF/versions/` |
| 50 |
# because bnd doesn't yet support multi-release jars. |
| 51 |
-fixupmessages.wrong.dir: "Classes found in the wrong directory: \\{META-INF/versions/...";is:=ignore |
| 52 |
|
| 53 |
# Don't scan for Class.forName package imports. |
| 54 |
# See https://bnd.bndtools.org/instructions/noclassforname.html |
| 55 |
-noclassforname: true |
| 56 |
|
| 57 |
# Don't add all the extra headers bnd normally adds. |
| 58 |
# See https://bnd.bndtools.org/instructions/noextraheaders.html |
| 59 |
-noextraheaders: true |
| 60 |
|
| 61 |
# Don't add the Private-Package header. |
| 62 |
# See https://bnd.bndtools.org/instructions/removeheaders.html |
| 63 |
-removeheaders: Private-Package |
| 64 |
|
| 65 |
# Instruct the APIGuardianAnnotations how to operate. |
| 66 |
# See https://bnd.bndtools.org/instructions/export-apiguardian.html |
| 67 |
-export-apiguardian: *;version=${'$'}{versionmask;===;${'$'}{version_cleanup;${'$'}{task.archiveVersion}}} |
| 68 |
""" |
| 69 |
) |
| 70 |
|
| 71 |
// Do the actual work putting OSGi stuff in the jar. |
| 72 |
doLast(buildAction()) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Bnd's Resolve task uses a properties file for its configuration. This |
| 77 |
// task writes out the properties necessary for it to verify the OSGi |
| 78 |
// metadata. |
| 79 |
val osgiProperties by tasks.registering(WriteProperties::class) { |
| 80 |
destinationFile = layout.buildDirectory.file("verifyOSGiProperties.bndrun") |
| 81 |
property("-standalone", true) |
| 82 |
project.extensions.getByType(JavaLibraryExtension::class.java).let { javaLibrary -> |
| 83 |
property("-runee", "JavaSE-${javaLibrary.mainJavaVersion}") |
| 84 |
} |
| 85 |
property("-runrequires", "osgi.identity;filter:='(osgi.identity=${project.name})'") |
| 86 |
property("-runsystempackages", "jdk.internal.misc,jdk.jfr,sun.misc") |
| 87 |
// API Guardian should be optional -> instruct resolver to ignore it |
| 88 |
// during resolution. Resolve should still pass. |
| 89 |
property("-runblacklist", "org.apiguardian.api") |
| 90 |
} |
| 91 |
|
| 92 |
val osgiVerification by configurations.creatingResolvable { |
| 93 |
extendsFrom(configurations.runtimeClasspath.get()) |
| 94 |
} |
| 95 |
|
| 96 |
// Bnd's Resolve task is what verifies that a jar can be used in OSGi and |
| 97 |
// that its metadata is valid. If the metadata is invalid this task will |
| 98 |
// fail. |
| 99 |
val verifyOSGi by tasks.registering(Resolve::class) { |
| 100 |
bndrun = osgiProperties.flatMap { it.destinationFile } |
| 101 |
outputBndrun = layout.buildDirectory.file("resolvedOSGiProperties.bndrun") |
| 102 |
isReportOptional = false |
| 103 |
// By default bnd will use jars found in: |
| 104 |
// 1. project.sourceSets.main.runtimeClasspath |
| 105 |
// 2. project.configurations.archives.artifacts.files |
| 106 |
// to validate the metadata. |
| 107 |
// This adds jars defined in `osgiVerification` also so that bnd |
| 108 |
// can use them to validate the metadata without causing those to |
| 109 |
// end up in the dependencies of those projects. |
| 110 |
bundles(osgiVerification) |
| 111 |
properties.empty() |
| 112 |
outputs.doNotCacheIf("https://github.com/bndtools/bnd/issues/5666") { true } |
| 113 |
} |
| 114 |
|
| 115 |
tasks.check { |
| 116 |
dependsOn(verifyOSGi) |
| 117 |
} |