Thursday, August 28, 2008

Sling OSGi Track pt 2: using Felix' bundle plugin for manifest entries

This is the continuation of  Sling OSGi Track pt 1: hand-rolled service bundle.

In this part, I will remove the manually created MANIFEST.MF, and have it created by the Maven2 felix-bundle-plugin.

Aims:

  • define OSGi-specific manifest entries in pom.xml and let Maven create the manifest

Ingredients:

Files:

Outline:

  • enable and configure the plugin in pom.xml
  • change the packaging style in pom.xml
  • stop merging our own manifest into the created jar and remove manifest file

Execution:

enable and configure the plugin in pom.xml

The plugin configuration is done in lines 20-35.
(27): Export-Package creates and Export-Package manifest entry with the same content
(28): same for Import-Package
(29): the symbolic name is taken from the pom's articactId
(30): the bundle name is taken from the pom's project name
(32): Bundle-Activator -- the main class

change the packaging style in pom.xml

We need to change the packaging style to "bundle" (defaults to "jar") for the plugin to do its work.
This is done in line 8.

  1: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2: 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3: 	<modelVersion>4.0.0</modelVersion>
  4: 	<groupId>mh.studies</groupId>
  5: 	<artifactId>mh.studies.sling.osgitest</artifactId>
  6: 	<name>OSGI Test Bundle</name>
  7: 	<version>0.0.2</version>
  8: 	<packaging>bundle</packaging>
  9: 	<description />
 10: 	<build>
 11: 		<plugins>
 12: 			<plugin>
 13: 				<groupId>org.apache.maven.plugins</groupId>
 14: 				<artifactId>maven-compiler-plugin</artifactId>
 15: 				<configuration>
 16: 					<source>1.5</source>
 17: 					<target>1.5</target>
 18: 				</configuration>
 19: 			</plugin>
 20: 			<plugin>
 21: 				<groupId>org.apache.felix</groupId>
 22: 				<artifactId>maven-bundle-plugin</artifactId>
 23: 				<version>1.4.3</version>
 24: 				<extensions>true</extensions>
 25: 				<configuration>
 26: 					<instructions>
 27: 						<Export-Package>mh.osgitest</Export-Package>
 28: 						<Import-Package>org.osgi.framework;version="1.3.0"</Import-Package>
 29: 						<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
 30: 						<Bundle-Name>${pom.name}</Bundle-Name>
 31: 						<Bundle-Vendor>Moritz Havelock</Bundle-Vendor>
 32: 						<Bundle-Activator>mh.osgitest.Activator</Bundle-Activator>
 33: 						<Built-By>Moritz Havelock</Built-By>
 34: 					</instructions>
 35: 				</configuration>
 36: 			</plugin>
 37: 		</plugins>
 38: 	</build>
 39: 	<dependencies>
 40: 		<dependency>
 41: 			<groupId>org.apache.felix</groupId>
 42: 			<artifactId>org.osgi.core</artifactId>
 43: 			<version>1.0.1</version>
 44: 		</dependency>
 45: 	</dependencies>
 46: </project>

stop merging our own manifest into the created jar and remove manifest file

We can now remove the file src/main/resources/META-INF/MANIFEST.MF, as this will be auto-generated.
Also, we can remove the following section from the pom.

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<archive>
						<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
					</archive>
				</configuration>
			</plugin>
 
Build

Do an "mvn clean package".

Now, open the created jar file ( target/mh.studies.sling.osgitest-0.0.2.jar ), and have a look at the created META-INF/MANIFEST.MF:
as you can see, all the lines we previously created manually are there, looking much the same.
There are slight differences in (7) -- the used packages are declared, and (3), (2), (11) and (15) are new.

  1: Manifest-Version: 1.0
  2: Built-By: Moritz Havelock
  3: Created-By: Apache Maven Bundle Plugin
  4: Bundle-Activator: mh.osgitest.Activator
  5: Import-Package: org.osgi.framework;version="1.3.0"
  6: Bnd-LastModified: 1219937878156
  7: Export-Package: mh.osgitest;uses:="org.osgi.framework"
  8: Bundle-Version: 0.0.2
  9: Bundle-Name: OSGI Test Bundle
 10: Bundle-ClassPath: .
 11: Build-Jdk: 1.5.0_16
 12: Bundle-ManifestVersion: 2
 13: Bundle-Vendor: Moritz Havelock
 14: Bundle-SymbolicName: mh.studies.sling.osgitest
 15: Tool: Bnd-0.0.255

 

Install / Test Client

To install the bundle, go to the system console at http://localhost:7402/system/console/list.

Then, call the URL http://localhost:7402/test .

For the detail of these two steps, have a look at the previous post: /2008/08/basic-osgi-service-for-sling.html

No comments: