Gradle has an OsgiManifest
class, which is an extended jar manifest:
https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/osgi/OsgiManifest.html
There is a post on StackOverflow that shows similar usage:
How to add Import-Package instructions for runtime dependencies?
The relevant gradle block looks like this:
apply plugin: 'java'
apply plugin: 'osgi'
jar {
baseName = 'awesome'
manifest {
name = 'An Awesome Application'
symbolicName = 'com.example.awesome'
instruction 'Import-Package', 'org.springframework.orm', '*'
}
}
If you have existing manifests and would like to use your own custom files, you can do this by setting the manifest file location within the jar closure:
jar {
manifest {
def manif = "${resourcesDir}/MANIFEST.MF"
if (new File(manif).exists()) {
from file(manif)
}
else{
name = 'overwrittenSpecialOsgiName'
instruction 'Private-Package', 'org.mycomp.somepackage'
instruction 'Bundle-Vendor', 'MyCompany'
instruction 'Bundle-Description', 'Platform2: Metrics'
}
}
}
The documentation for Gradle's manifest can be found here:
https://docs.gradle.org/current/javadoc/org/gradle/api/java/archives/Manifest.html
For your 'other question':
There are additional Gradle plugins for building OSGI bundles, in some cases, including dependencies from other OSGI bundles.
For one example, there is the Gradle Bundle Plugin, which uses the bnd tool and allows you to specify to include transitive-dependencies and even exclude unwanted ones.
As an example:
jar {
manifest {
attributes 'Implementation-Title': 'Bundle Quickstart', // Will be added to manifest
'Import-Package': '*' // Will be overwritten by the instuctions below
}
}
bundle {
includeTransitiveDependencies = true
instructions << [
'Bundle-Activator': 'foo.bar.MyBundleActivator',
'Import-Package': 'foo.*',
'-sources': true
]
instruction 'Export-Package', '*' // Specify an individual instruction
instruction '-wab', ''
}
There is also the Gradle osgi-run plugin, which includes transitive dependencies by default:
dependencies {
// all your usual dependencies
...
osgiRuntime( "your:dependency:1.0" ) {
transitive = false // transitive dependencies not included in OSGi runtime
}
}
Hopefully that's enough to get you going.
build.gradle
jar section, that is huge monkey work. Those files may change before gradle build will start working and accepted as main delivery method. – Tussisjar
command from the JDK takes a manifest file using the-m
switch, I would be very surprised if the Gradle task did not expose that option in some way. – Gaminjar
command allows a predefined manifest to be supplied as a file. Quickly checking the Gradle docs, it looks like the Jar task has amanifest
property that you can use. Something likeManifest.from(filename)
. – Gamin