Skip to content

Commit

Permalink
Adds Automatic-Module-Name and OSGi bundle attributes to manifest (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt authored Jan 5, 2024
1 parent 9f828ec commit a7e0de7
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import com.github.jk1.license.filter.LicenseBundleNormalizer
import com.github.jk1.license.render.InventoryMarkdownReportRenderer
import com.github.jk1.license.render.TextReportRenderer
import org.gradle.kotlin.dsl.support.unzipTo
import org.gradle.kotlin.dsl.support.zipTo
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
import proguard.gradle.ProGuardTask
Expand Down Expand Up @@ -33,6 +35,12 @@ plugins {

// Used for generating the third party attribution document
id("com.github.jk1.dependency-license-report") version "2.5"

// Used for generating OSGi bundle information
// We cannot use the latest version since it targets JDK 17, and we don't want to force building with JDK 17
// Without `apply false`, the plugin is automatically applied to the main "jar" task, which somehow interferes with
// the "spotbugsMain" task, causing it to fail. Instead, we will create a separate task to generate the bundle info.
id("biz.aQute.bnd.builder") version "6.4.0" apply false
}

jacoco {
Expand Down Expand Up @@ -109,12 +117,56 @@ tasks {
archiveClassifier.set("original")
}

val generateManifest = create<aQute.bnd.gradle.Bundle>("generateManifest") {
// Create the manifest using the same sources as the regular jar.
from(jar.get().source)
archiveClassifier.set("manifest")

manifest {
attributes(
"Automatic-Module-Name" to "com.amazon.ion",
"Main-Class" to "com.amazon.ion.impl._Private_CommandLine",
"Build-Time" to "${Instant.now()}",
"Build-Version" to "$version",
)
}
// Sets OSGi bundle attributes
// See https://en.wikipedia.org/wiki/OSGi#Bundles for a minimal introduction to the bundle manifest
// See https://enroute.osgi.org/FAQ/520-bnd.html for a high level of what is the "bnd" tool
// If we ever expose any shaded classes, then the bundle info will need to be added after the shadow step.
// For now, though, generating the bundle info here results
bundle {
bnd(
"Bundle-License: https://www.apache.org/licenses/LICENSE-2.0.txt",
// These must be specified manually to keep the values the same as the pre-v1.9.5 values.
// What will happen if they change? I don't know, but possibly some sort of compatibility problem.
"Bundle-Name: com.amazon.ion:ion-java",
"Bundle-SymbolicName: com.amazon.ion.java",
// Exclusions must come first when specifying exports.
// We exclude the `apps`, `impl`, and `shaded_` packages and expose everything else.
"Export-Package: !com.amazon.ion.apps.*,!com.amazon.ion.impl.*,!com.amazon.ion.shaded_.*,com.amazon.ion.*",
// Limit imports to only this package so that we don't add in any kotlin imports.
// This line is not necessary if we create bundle info after shading (but that is more complex).
"Import-Package: com.amazon.ion.*",
// Removing the 'Private-Package' header because it is optional and was not present prior to v1.9.5
"-removeheaders: Private-Package",
)
}
// This is not strictly necessary, but it is nice to make the output of this task into a manifest-only jar.
doLast {
val temp = temporaryDir
unzipTo(temp, outputs.files.singleFile)
delete(fileTree(temp).matching { excludes.add("META-INF/MANIFEST.MF") })
zipTo(outputs.files.singleFile, temp)
}
}

// Creates a super jar of ion-java and its dependencies where all dependencies are shaded (moved)
// to com.amazon.ion_.shaded.are_you_sure_you_want_to_use_this
shadowJar {
val newLocation = "com.amazon.ion.shaded_.do_not_use"
archiveClassifier.set("shaded")
dependsOn(generateLicenseReport)
dependsOn(generateManifest, generateLicenseReport)
from(generateLicenseReport.get().outputFolder)
relocate("kotlin", "$newLocation.kotlin")
relocate("org.jetbrains", "$newLocation.org.jetbrains")
Expand All @@ -127,6 +179,7 @@ tasks {
// Eliminate dependencies' pom files
exclude("**/pom.*")
}
manifest.inheritFrom(generateManifest.manifest)
}

/**
Expand Down

0 comments on commit a7e0de7

Please sign in to comment.