Skip to content

Commit

Permalink
[osgi] Optionally import org.apiguardian.api
Browse files Browse the repository at this point in the history
Fixes #2547.
  • Loading branch information
kriegfrj authored and marcphilipp committed Jun 19, 2021
1 parent b7499dc commit 5bc261a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
8 changes: 7 additions & 1 deletion buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// intentionally left blank
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
16 changes: 14 additions & 2 deletions buildSrc/src/main/kotlin/osgi-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ plugins {
`java-library`
}

// Because the current build version of apiguardian (1.1.1) doesn't have OSGi metadata, bnd
// cannot automatically determine the correct version to use in the Import-Package statement.
// Fetch the current version out of the libs.versions.toml file.
val apiguardianVersion = project.extensions.getByType(VersionCatalogsExtension::class).named("libs").findVersion("apiguardian").get()
val importAPIGuardian = "org.apiguardian.*;version=\"\${range;[==,+);${apiguardianVersion}}\";resolution:=\"optional\""

// This task enhances `jar` and `shadowJar` tasks with the bnd
// `BundleTaskConvention` convention which allows for generating OSGi
// metadata into the jar
Expand All @@ -14,6 +20,8 @@ tasks.withType<Jar>().matching {
}.configureEach {
val btc = BundleTaskConvention(this)

extra["importAPIGuardian"] = importAPIGuardian

// These are bnd instructions necessary for generating OSGi metadata.
// We've generalized these so that they are widely applicable limiting
// module configurations to special cases.
Expand All @@ -28,14 +36,15 @@ tasks.withType<Jar>().matching {
# These are the general rules for package imports.
Import-Package: \
!org.apiguardian.api,\
${importAPIGuardian},\
org.junit.platform.commons.logging;status=INTERNAL,\
kotlin.*;resolution:="optional",\
*
# This tells bnd not to complain if a module doesn't actually import
# the kotlin packages, but enough modules do to make it a default.
# the kotlin and apiguardian packages, but enough modules do to make it a default.
-fixupmessages.kotlin.import: "Unused Import-Package instructions: \\[kotlin.*\\]";is:=ignore
-fixupmessages.apiguardian.import: "Unused Import-Package instructions: \\[org.apiguardian.*\\]";is:=ignore
# This tells bnd to ignore classes it finds in `META-INF/versions/`
# because bnd doesn't yet support multi-release jars.
Expand Down Expand Up @@ -81,6 +90,9 @@ val osgiProperties by tasks.registering(WriteProperties::class) {
}
property("-runrequires", "osgi.identity;filter:='(osgi.identity=${project.name})'")
property("-runsystempackages", "jdk.internal.misc,jdk.jfr,sun.misc")
// API Guardian should be optional -> instruct resolver to ignore it
// during resolution. Resolve should still pass.
property("-runblacklist", "org.apiguardian.api")
}

val osgiVerification by configurations.creating {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tasks.jar {
bnd("""
# Import JUnit4 packages with a version
Import-Package: \
!org.apiguardian.api,\
${extra["importAPIGuardian"]},\
org.junit;version="[${libs.versions.junit4Min.get()},5)",\
org.junit.platform.commons.logging;status=INTERNAL,\
org.junit.rules;version="[${libs.versions.junit4Min.get()},5)",\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ tasks {
bnd("""
# Customize the imports because this is an aggregate jar
Import-Package: \
!org.apiguardian.api,\
${extra["importAPIGuardian"]},\
kotlin.*;resolution:="optional",\
*
# Disable the APIGuardian plugin since everything was already
Expand Down
2 changes: 1 addition & 1 deletion junit-platform-runner/junit-platform-runner.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tasks.jar {
bnd("""
# Import JUnit4 packages with a version
Import-Package: \
!org.apiguardian.api,\
${extra["importAPIGuardian"]},\
org.junit.platform.commons.logging;status=INTERNAL,\
org.junit.runner.*;version="[${libs.versions.junit4Min.get()},5)",\
*
Expand Down
2 changes: 1 addition & 1 deletion junit-vintage-engine/junit-vintage-engine.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tasks {
bnd("""
# Import JUnit4 packages with a version
Import-Package: \
!org.apiguardian.api,\
${extra["importAPIGuardian"]},\
junit.runner;version="[${junit4Min},5)",\
org.junit;version="[${junit4Min},5)",\
org.junit.experimental.categories;version="[${junit4Min},5)",\
Expand Down

0 comments on commit 5bc261a

Please sign in to comment.