Migration from Gradle with Bnd to Maven Bnd

Hi,

Firstly, I am sorry for the confusing title of the question as I am new to BnD and I am also confused now. So, it would be very grateful if you could help me out.

Now, we have many OSGI bundles with maven using its pom file. Each OSGI bundle is separated with its maven pom file. Below is code snippet from one of pom file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>abc.abc.abc</groupId>
	<artifactId>BundleName</artifactId>
	<version>${revision}</version>
	<packaging>jar</packaging>
	<name>BundleName</name>
	<description></description>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<revision>2.0.0-SNAPSHOT</revision>
		<Bundle-Category></Bundle-Category>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>5.1.1</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Name>${project.name}</Bundle-Name>
						<Bundle-Description>${project.description}</Bundle-Description>
						<Bundle-Vendor>${project.organization.name}</Bundle-Vendor>
						<Bundle-Version>${project.version}</Bundle-Version>
						<Bundle-Activator>a.b.cActivator</Bundle-Activator>
						<Bundle-ClassPath>.,{maven-dependencies}</Bundle-ClassPath>
						<!-- generic imports for our osgi bundles, add your own -->
						<Import-Package>
							javax.activation, 
							org.company.category.something
						</Import-Package>
						<Bundle-RequiredExecutionEnvironment>JavaSE-1.6,JavaSE-1.7</Bundle-RequiredExecutionEnvironment>
						<!-- set your export packages here -->
						<Export-Package>
							org.company.category.something.*
                       </Export-Package>
						<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
						<Embed-Directory>lib</Embed-Directory>
						<Require-Bundle>jaxb-api;bundle-version="2.2.1"</Require-Bundle>
					</instructions>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
        </plugins>
	</build>
	<dependencies>
		 <dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

As each bundle will be built into a jar file separatedly by running “mvn clean install”, I have to deploy them one by one manually to combine an complete application (a group of many bundles). And this approach can not help to build a single executable jar file.

Now, I just figure out BnD can help me to group all the bundle and build them into a single executable jar file. I am planning to find a way to migrate the existing maven pom file with maven command to work with bnd.

I have tried Bnd Workspace, Gradle build but it brings me some unknown issues from IDE Eclipse that I have no idea how to fix them. So, I am trying to find a way to use maven to build my code like now. I heard there is a plugin called bnd-maven-plugin but the Github README does not provide a clear guideline how to migrate it from a maven pom file. Below is my try but it does not work. It seems that everything under bnd is ignored when packing, no exporting or importing classes in the Jar file…

			<plugin>
				<!-- New for BND-->
				<groupId>biz.aQute.bnd</groupId>
				<artifactId>bnd-maven-plugin</artifactId>
				<configuration>
					<bnd><![CDATA[
						Bundle-Activator: abc.ac.ac.Activator
						Import-Package: \
							abc.bac.states;'resolution:'=optional
						Export-Package: \
							abc.a.fds..*
					]]></bnd>
				</configuration>
			</plugin>

I am quite new to bnd so it would be very grateful if you could give me a guideline how to ultilize my existing maven build with pom file to work with bnd. Thank you so much!

Regards,
Viet

You can find a nice example here: osgi-test/examples/osgi-test-example-mvn at main · osgi/osgi-test · GitHub

I may need to see the full pom to get an idea, why the configuration seems to be ignored. 2 Things jump at me though:

  1. The bnd config as this blob in the pom.xml is always a bit ugly. You can put a bnd.bnd file next to your pom and do the configuration in there. Works much better.
  2. The Config itself:
Bundle-Activator: abc.ac.ac.Activator
Import-Package: \
    # The resolution was quoted wrongly in your example
    abc.bac.states;resolution:=optional,\
    # This is the catch all that tells bnd to calculate the rest of the Imports
    *
-exportpackge:\
    # There was a second dot in your statement, not sure what that did
    abc.a.fds.*

I hope that helps a bit.

1 Like