# The OSGi Plugin

⚠ | This plugin is deprecated and will be removed in the next major Gradle release. New builds should not use this plugin. The OSGi plugin makes heavy use of the BND tool. A separate plugin implementation is maintained by the BND authors that has more advanced features.

The OSGi plugin provides a factory method to create an OsgiManifest object. `OsgiManifest` extends Manifest. To learn more about generic manifest handling, see more about Java manifests. 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.

## Usage

To use the OSGi plugin, include the following in your build script:

Example 1. Using the OSGi plugin
build.gradle

    plugins {
        id 'osgi'
    }

build.gradle.kts

    plugins {
        osgi
    }

## Implicitly applied plugins

Applies the Java base plugin.

## Tasks

The OSGi plugin adds the following tasks to the project:

`osgiClasses` — Sync

  Depends on: `classes`

  Copies all classes from the main source set to a single directory that is processed by BND.

## Convention object

The OSGi plugin adds the following convention object: OsgiPluginConvention

### Convention properties

The OSGi plugin does not add any convention properties to the project.

### Convention methods

The OSGi plugin adds the following methods. For more details, see the API documentation of the convention object.

Table 1. OSGi methods
Method | Return Type | Description
--- | --- | ---
osgiManifest() | OsgiManifest | Returns an OsgiManifest object.
osgiManifest(Closure cl) | OsgiManifest | Returns an OsgiManifest object configured by the closure.

The classes in the classes dir are analyzed regarding their 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.

Example 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'
        }
    }

build.gradle.kts

    tasks.withType<Jar>().configureEach {
        manifest {
            // the manifest of the default jar is of type OsgiManifest
            (manifest as? OsgiManifest)?.apply {
                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")
            }
        }
    }
    tasks.register<Jar>("fooJar") {
        manifest = osgi.osgiManifest {
            instruction("Bundle-Vendor", "MyCompany")
        }
    }

The first argument of the instruction call is the key of the property. The other arguments form the value. To learn more about the available instructions have a look at the BND tool.