第三十七章. OSGi 插件

Chapter 37. The OSGi Plugin

OSGi 插件提供了工厂方法来创建一个OsgiManifest对象。OsgiManifest 继承自 Manifest。要了解常见的清单处理的更多信息,请参阅第 23.13.1节,“Manifest”。如果应用了 Java 插件,OSGi 插件将把默认 jar 的 manifest 对象替换为一个OsgiManifest对象。被替换的manifest 会被合并到新的对象单中。
The OSGi plugin provides a factory method to create an OsgiManifest object. OsgiManifest extends Manifest. To learn more about generic manifest handling, see Section 23.13.1, “Manifest”. If the Java plugins is applied, the OSGi plugin replaces the manifest object of the default jar with an OsgiManifest object. The replaced manifest is merged into the new one.

OSGi 插件使 Peter Kriens BND tool 大量使用。
The OSGi plugin makes heavy use of Peter Kriens BND tool.

37.1. 用法

37.1. Usage

要使用 OSGi 插件,请在构建脚本中包含以下语句:
To use the OSGi plugin, include in your build script:

示例 37.1. 使用 OSGi 插件 - Example 37.1. Using the OSGi plugin

build.gradle

apply plugin: 'osgi'

37.2. 隐式应用插件

37.2. Implicitly applied plugins

适用于 Java 基础插件。
Applies the Java base plugin.

37.3. 任务

37.3. Tasks

此插件不会添加任何任务。
This plugin does not add any tasks.

37.4. 依赖管理

37.4. Dependency management

待决定
TBD

37.5. 约定对象

37.5. Convention object

OSGi 插件添加了下列约定对象: OsgiPluginConvention
The OSGi plugin adds the following convention object: OsgiPluginConvention

37.5.1. 约定属性

37.5.1. Convention properties

OSGi 插件没有向 project 添加任何的公约属性。
The OSGi plugin does not add any convention properties to the project.

37.5.2. 约定方法

37.5.2. Convention methods

OSGi 插件添加了以下方法。有关更多详细信息,请参见约定对象的 API 文档。
The OSGi plugin adds the following methods. For more details, see the API documentation of the convention object.

表 37.1. OSGi 方法 - Table 37.1. OSGi methods

方法
Method
返回类型
Return Type
描述
Description
osgiManifest() OsgiManifest 返回一个 OsgiManifest 对象。
Returns an OsgiManifest object.
osgiManifest(Closure cl) OsgiManifest 返回一个通过闭包配置的 OsgiManifest 对象。
Returns an OsgiManifest object configured by the closure.

在classes 目录下的类文件会被分析出关于它们的包的依赖,以及它们所公布的包名。并基于此计算 OSGi Manifest 中Import-PackageExport-Package的值。如果 classpath 中包含了 jar 包和 OSGi bundle,bundle 信息会被用来指定 Import-Package的值的版本信息。在 OsgiManifest 对象的显式属性旁边,你可以添加instructions。
The classes in the classes dir are analyzed regarding there package dependencies and the packages they expose. Based on this the Import-Package and the Export-Package values of the OSGi Manifest are calculated. If the classpath contains jars with an OSGi bundle, the bundle information is used to specify version information for the Import-Package value. Beside the explicit properties of the OsgiManifest object you can add instructions.

示例 37.2. OSGi MANIFEST.MF 文件配置 - Example 37.2. Configuration of OSGi MANIFEST.MF file

build.gradle

jar {
    manifest { // the manifest of the default jar is of type OsgiManifest
        name = 'overwrittenSpecialOsgiName'
        instruction 'Private-Package',
                'org.mycomp.package1',
                'org.mycomp.package2'
        instruction 'Bundle-Vendor', 'MyCompany'
        instruction 'Bundle-Description', 'Platform2: Metrics 2 Measures Framework'
        instruction 'Bundle-DocURL', 'http://www.mycompany.com'
    }
}
task fooJar(type: Jar) {
    manifest = osgiManifest {
        ~instruction 'Bundle-Vendor', 'MyCompany'
    }
}

instruction 调用的第一个参数是属性的键。其他参数构成了它的值。他们由 Gradle 使用,分隔符连接。要了解更多关于 instructions 的信息,可以看看BND tool
The first argument of the instruction call is the key of the property. The other arguments form the value. They are joined by Gradle with the , separator. To learn more about the available instructions have a look at the BND tool.