Issue with adding jar in OSGI bundle using <Embed-Dependency> - maven

I'm trying to embed third party libs and application jar in a OSGI bundle.I read the felix maven plugin document and tried using Embed-Dependency. But it doesn't seem to have any effect. Here's my pom
<dependencies>
&ltdependency>
&ltgroupId>com.test</groupId>
<artifactId>taxonomymessagebundle</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.0.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>com.test.taxonomy.dao.*;version=1.0.0</Export-Package>
<Import-Package>*</Import-Package>
</instructions>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
</configuration>
</plugin>
</plugins>
</build>
I'm mvn clean install to build the bundle. After the install, I took a look into the manifest file, it doesn't show any Bundle-Classpath or Embed information. Looks like it completely ignored the instruction. Also, the two dependent jars were not embedded as well in the bundle.
Here's the generated manifest:
code>
Manifest-Version: 1.0
Export-Package: com.test.taxonomy.dao;uses:="com.autodesk.taxonomy";version="1.0.0"
Bundle-Version: 1.0.0
Build-Jdk: 1.6.0_21
Built-By: bandops
Tool: Bnd-0.0.357
Bnd-LastModified: 1307492329392
Bundle-Name: Taxonomy Dao Bundle
Bundle-ManifestVersion: 2
Created-By: Apache Maven Bundle Plugin
Import-Package: com.test.taxonomy.dao;version="1.0",com.autodesk.test.message
Bundle-SymbolicName: com.test.taxonomy.daobundle
Any pointers will be appreciated.
-Thanks

The <Embed-Dependency> and <Embed-Transitive> should both be inside the <instructions> tag.

Related

Not able to connect Google Cloud Datastore with AEM 6.5 : Bundle has resolved dependencies

I have a requirement to connect google cloud datastore from AEM. I have added the dependencies in main pom and core pom.
MAIN POM
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>16.4.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>de here
Core POM
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datastore</artifactId>
</dependency>
When I deploy my bundle is in insatlled state and has the following errors
com.google.auth -- Cannot be resolved
com.google.auth.oauth2 -- Cannot be resolved
com.google.cloud -- Cannot be resolved
com.google.cloud.datastore -- Cannot be resolved
try adding those libs to the embed-dependencies in the POM. Also check that your libs are inside the .jar file generated.
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<!-- <Embed-Dependency> artifactId1, artifactId2;inline=true </Embed-Dependency> -->
<Export-Package>we.retail.core.model*</Export-Package>
<Private-Package>we.retail.core*</Private-Package>
<Sling-Model-Packages>
we.retail.core.model
</Sling-Model-Packages>
</instructions>
</configuration>
</plugin>
In the embed you can try something like: google-cloud-datastore
Also take care with the import type that you're using.
Link to Maven
Hope you can find the answer
If you do not need those, exclude them from the <Import-Packages>:
<Import-Package>
!com.google.auth.*,
*
</Import-Package>

NoClassDefFoundError using Xerces in OSGi environment

I've built a simple bundle which instantiate a org.apache.xerces.util.XMLCatalogResolver class.
In my pom, I've had these dependencies and plugin :
<build>
<plugins>
<!-- Plugin to create bundle -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>*</Import-Package>
<Export-Package>myXMLResolver</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.16.1</version>
<type>bundle</type>
<scope>provided</scope>
</dependency>
<!-- xerces is provided in /lib/endorsed/ folder of ServiceMix -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.xmlresolver</artifactId>
<version>1.2_5</version>
<type>bundle</type>
<scope>compile</scope>
</dependency>
</dependencies>
</build>
Testing the bundle "locally" (within the IDE) is working fine.
Deploying it into the /deploy folder is also fine (its status is "Active").
When running it (through Camel route), I get this stacktrace :
Caused by: java.lang.NoClassDefFoundError: org/apache/xml/resolver/CatalogManager
at org.apache.xerces.util.XMLCatalogResolver.init(Unknown Source)[:]
at org.apache.xerces.util.XMLCatalogResolver.<init>(Unknown Source)[:]
at org.apache.xerces.util.XMLCatalogResolver.<init>(Unknown Source)[:]
at myXMLResolver.MainBean.run(MainBean.java:17)[241:test.myXMLResolver:0.0.1.SNAPSHOT]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_95]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_95]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_95]
at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_95]
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)[199:org.apache.camel.camel-core:2.16.1]
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)[199:org.apache.camel.camel-core:2.16.1]
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)[199:org.apache.camel.camel-core:2.16.1]
... 19 more
If I'm not wrong, the class CatalogManager from package org.apache.xml.resolver existed during compilation but not in run-time. So I deploy org.apache.servicemix.bundles.xmlresolver dependency into /deploy folder.
When executing in Karaf console the command bundle:headers <xmlresolver_bundle_id>, I get :
Export-Package =
...
org.apache.xml.resolver;uses:="org.apache.xml.resolver.helpers,javax.xml.parsers,org.apache.xml.resolver.readers";version=1.2,
...
Nevertheless, same stacktrace appears.
Even if I add the package org.apache.xml.resolver in the <Import-Package> I get the same error.
Any idea of what's going on ?
I'm using ServiceMix 6.1.0 (w/ Karaf 3.0.5), running on Java 7.
Edit :
Here is the bundle's MANIFEST.MF :
Manifest-Version: 1.0
Bnd-LastModified: 1461311084908
Build-Jdk: 1.8.0_72-internal
Bundle-ManifestVersion: 2
Bundle-Name: myXMLResolver
Bundle-SymbolicName: test.myXMLResolver
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: myXMLResolver;version="0.0.1";uses:="org.apache.camel,org.apache.xerces.util"
Import-Package: org.apache.camel;version="[2.16,3)",org.apache.xerces.util;version="[2.11,3)"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Tool: Bnd-2.4.1.201501161923
If I add the package org.apache.xml.resolver in the <Import-Package>, Import-Package from MANIFEST become : org.apache.camel;version="[2.16,3)",org.apache.xerces.util;version="[2.11,3)",org.apache.xml.resolver;version="[1.2,2)"
Edit #2 : workaround
A workaround can be done :
Put the bundle org.apache.servicemix.bundles.xmlresolver in
<SMX_HOME>/lib/endorsed folder
In the file <SMX_HOME>/etc/config.properties, add
org.apache.xml.resolver to the property
org.osgi.framework.system.package.extra.
But it doesn't look like a clean method...
Edit #3 : issue opened on Jira : https://issues.apache.org/jira/browse/SM-3020

Apache Felix Maven Bundle Plugin avoid inlining of dependencies

How can I achieve that the plugin does not inline the dependencies in the new build jar file?
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Category>tools</Bundle-Category>
<Fragment-Host>org.jsmpp.jsmpp</Fragment-Host>
<Private-Package>!</Private-Package>
<Export-Package>
org.jsmpp.*;version="2.2.3"
</Export-Package>
<Import-Package>!org.slf4j</Import-Package>
<Bundle-Version>2.2.3</Bundle-Version>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jsmpp</groupId>
<artifactId>jsmpp</artifactId>
</dependency>
</dependencies>
The plugin does not inline any dependencies, unless you include an Embed-Dependency instruction. That instruction can be inherited from a parent POM.
All packages that match the <Export-Package> instruction are included in the bundle, even if those packages come from a dependency. So you can either specify all packages from your bundle explicitly, or use a wildcard and exclude unwanted packages with the '!' prefix, e.g.
<Export-Package>
org.jsmpp.*;version="2.2.3",
!org.jsmpp.donotwant
</Export-Package>
see maven-bundle-plugin documentation
Use _exportcontents instead of Export-Package.
_exportcontents affect only the manifest, whereas Export-Package modify the manifest and the content of your bundle.
see: http://www.aqute.biz/Bnd/Format

How to embed a library JAR in an OSGi bundle using Tycho

I am using Maven with the Tycho plugin to build my OSGi bundles.
In one of my bundles, I use the facebook API through the restfb-1.7.0.jar library.
For now, it is directly placed on the classpath (in Eclipse) and embedded in the effective OSGi bundle jar file with following build.properties configuration:
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
lib/restfb-1.7.0.jar
Now I would like to have this restfb lib downloaded from Maven (e.g. as dependency) and embedded into my OSGi bundle jar. Is it possible with Maven/Tycho? How?
You need the following configuration to embed a JAR into an OSGi plugin with Tycho:
In the pom.xml, configure the copy goal of the maven-dependency-plugin
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-libraries</id>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<item>
<groupId>com.restfb</groupId>
<artifactId>restfb</artifactId>
<version>1.7.0</version>
</item>
</artifactItems>
<outputDirectory>lib</outputDirectory>
<stripVersion>true</stripVersion>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Edit the MANIFEST.MF to have the library added to the OSGi bundle classpath
Bundle-ClassPath: ., lib/restfb.jar
Edit the build.properties to have the library included in the JAR packaged by Tycho
bin.includes = META-INF/,\
.,\
lib/restfb.jar
I think what you would want is to have a dependency in the POM with the compile time scope like the example below: use the correct artifact and version info to get the item you want. you should investigate the dependencies section of the maven ref for poms
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>compile</scope>
</dependency>

How to package dependencies with maven nbm plugin

I have modified netbeans module jar that I want to package with my module. How do I do this ?
Dependency in context
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-modules-db-dataview</artifactId>
<version>${netbeans.platform.version}</version>
<scope>system</scope>
<systemPath>${dbdataview}</systemPath>
</dependency>
mvn nbm configuration
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<version>3.5</version>
<extensions>true</extensions>
<configuration>
<additionalArguments>${netbeans.run.params.ide}</additionalArguments>
<netbeansInstallation>/home/venkat/netbeans-7.1.1/</netbeansInstallation>
<keystore>keystore</keystore>
<keystorealias>ezondaice</keystorealias>
<keystorepassword>ezondaice</keystorepassword>
</configuration>
</plugin>
Edit
I have the following dependency as well. This one seems to get packaged with the nbm inside
/netbeans/modules/ext/org.yaml/. Not sure why the other dependency is not being packaged into nbm.
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.11-SNAPSHOT</version>
</dependency>

Resources