第三十七章. OSGi 插件

Chapter 37. The OSGi Plugin

OSGi 插件提供了一种工厂方法来创建 OsgiManifest 对象。OsgiManifest扩展自Manifest。要了解有关通用清单处理的更多信息,请参阅《第23.13.1节,“清单”》。如果应用了 Java 插件,则 OSGi 插件会把默认 jar 的 manifest 对象替换为一个 OsgiManifest 对象。替换后的清单会被合并到新的清单中。
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 插件没有向项目添加任何的公约属性。
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.

在 classees 目录中的类将被分析出关于它们的包的依赖,以及它们所公布的包。并基于此,计算出 OSGi 清单中 Import-PackageExport-Package 的值。如果类路径中包含了带有 OSGi bundle 的 jar,则 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 使用 , 分隔符对它们进行连接。要了解有关可用的 instruction 的更多信息,请查看 BND 工具
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.