Following on from OSGI bundle dependencies
I have reverted maven-bundle-plugin back to using the defaults. Here is my current pom:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.felix.test</groupId>
<artifactId>com.felix.test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Everything bundles and installs OK. When I try to start the bundle I'm told I'm missing net.sf.ehcache which I install. Then I'm missing slf4j.api which I install. Then I'm missing slf4j.impl and I have tried installing pretty much every slf4j.impl possibility from https://jpm4j.org/#!/ but most (slf4j-simple-1.7.12.jar, slf4j-log4j12-1.7.12.jar) report back:
org.osgi.framework.BundleException: Fragment bundles can not be
started.
This is my current error from GoGo:
org.osgi.framework.BundleException: Unable to resolve com.felix.test
[16](R 16.0): missing requirement [com.felix.test [16](R 16.0)]
osgi.wiring.package;
(&(osgi.wiring.package=net.sf.ehcache)(version>=2.10.0)(!(version>=3.0.0)))
[caused by: Unable to resolve net.sf.ehcache [17](R 17.0): missing
requirement [net.sf.ehcache [17](R 17.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j)(version>=1.7.0)(!(version>=2.0.0)))
[caused by: Unable to resolve slf4j.api [23](R 23.0): missing
requirement [slf4j.api [23](R 23.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0))]] Unresolved
requirements: [[com.felix.test [16](R 16.0)] osgi.wiring.package;
(&(osgi.wiring.package=net.sf.ehcache)(version>=2.10.0)(!(version>=3.0.0)))]
Hopefully I'm getting closer...
Thank you!
You have two errors to address. The first is "fragment bundles cannot be started". The error message tells you everything you need to know. The slf4j implementation bundles are fragments, and you cannot start fragments. So just don't start them!
You haven't specified how you are running your OSGi Framework, but somewhere you must have some code that iterates over all the installed bundles and calls the start() method on each. You need to modify your code to not call start() on bundles that are fragments. You can tell if any bundle is a fragment as follows:
(bundle.adapt(BundleRevision.class).getTypes() | BundleRevision.TYPE_FRAGMENT) > 0;
OR:
bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null;
The second error says that the bundle named com.felix.test has a dependency on package net.sf.ehcache. I have never heard of com.felix.test... is this the bundle you are building? Do you actually use Ehcache in your code? If so, you obviously need to install the Ehcache bundle. If you do have Ehcache installed, then it might be the wrong version; your bundle requires version 2.10.0 up to but excluding 3.0.0.
Related
I'm trying to learn OSGI and figured I would build a simple rest application using Spark Servlet.
https://mvnrepository.com/artifact/com.sparkjava/spark-core/1.0
Within my maven build plugin, I embed Spark-Core. However, after I build and run the bundle, it tells me there is a wiring package problem. So I add the package import, rinse and repeat. I'll get a different wiring package problem, so then I add the dependency, etc.
This seems like a long tedious process to add one package after another. What's the correct way to do this?
POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>osgi-demo</artifactId>
<groupId>com.osgi-hacking</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>bundle</packaging>
<artifactId>osgiclient</artifactId>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-servlet</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>
${project.groupId}.${project.artifactId}
</Bundle-SymbolicName>
<Bundle-Name>
CUSTOM :: GREETER CLIENT :: BUNDLE
</Bundle-Name>
<Bundle-Version>
9.4.8.v20171121
</Bundle-Version>
<Bundle-Activator>
com.osgi.client.Activator
</Bundle-Activator>
<Embed-Dependency>
spark-core
</Embed-Dependency>
<Import-Package>
*
</Import-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The spark core jar is already a bundle.
So there is no need to embed it. Simply install it as well as its dependendencies (which is mainly jetty) in OSGi.
For your own bundle. Simply remove all inside . The defaults of the maven bundle plugin will produce what you need.
It will detect the packages you need and write Import-Package statements for them. When you then have spark-core installed as a bundle it should work fine.
Currently I am moving from karaf 3.0.5 to the newest version 4.0.2, I do assembly my own karaf with the karaf-maven-plugin. This is how my pom looks like.
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>my.own.group</groupId>
<artifactId>assemble</artifactId>
<version>1.14.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>karaf-customize</artifactId>
<modelVersion>4.0.0</modelVersion>
<packaging>karaf-assembly</packaging>
<dependencies>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>framework</artifactId>
<version>${karaf.version}</version>
<type>kar</type>
</dependency>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>standard</artifactId>
<classifier>features</classifier>
<version>${karaf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf.karaf</groupId>
<artifactId>apache-cxf</artifactId>
<classifier>features</classifier>
<version>${cxf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>enterprise</artifactId>
<classifier>features</classifier>
<version>${karaf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>my.own.group</groupId>
<artifactId>kar-archive</artifactId>
<version>1.14.0-SNAPSHOT</version>
<type>pom</type>
<optional>true</optional>
</dependency>
<dependency>
<groupId>my.own.group</groupId>
<artifactId>karaf-branding</artifactId>
<version>1.14.0-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.alutam</groupId>
<artifactId>ziputils</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<version>${karaf.version}</version>
<extensions>true</extensions>
<configuration>
<javase>1.8</javase>
<bootFeatures>
<feature>jasypt-encryption</feature>
<feature>config</feature>
<feature>standard</feature>
<feature>region</feature>
<feature>management</feature>
<feature>bundle</feature>
<feature>package</feature>
<feature>kar</feature>
<feature>ssh</feature>
<feature>http</feature>
<feature>cxf</feature>
<feature>service-wrapper</feature>
<feature>jdbc</feature>
<feature>system</feature>
</bootFeatures>
</configuration>
</plugin>
</plugins>
</build>
</project>
With this configuration I do get the following error for several dependencies.
Caused by: org.osgi.framework.BundleException: Unsupported 'Bundle-ManifestVersion' value: 1
at org.apache.karaf.features.internal.resolver.ResourceBuilder.doBuild(ResourceBuilder.java:88)
at org.apache.karaf.features.internal.resolver.ResourceBuilder.build(ResourceBuilder.java:78)
I guess it happens within this parser. The reason is some old third party libraries have only Bundle-ManifestVersion: 1 set within their manifest file.
With karaf-maven-plugin 3.x this didn't matter at all. In contrast the karaf-maven-plugin 4.x fails with message above.
The only way I know to fix this is either rebuild from source or repack the hole jar again.
Is there any other way like a configuration for the karaf-maven-plugin to disable this constraint check? Because it would be awful lot of work to get all of this bundles up an running, again.
I faced the same error when updating to Karaf 4 and you have two choices:
Osgify conflictive dependency using bndtools:
Download bnd tools
Open a shell where you have downloaded bnd-2.4.0.jar.
Type:
java -jar bnd-2.4.0.jar wrap -o osgify-dependency.jar dependency.jar
where dependency.jar is your third party and osgify-dependency.jar will be the output.
Deploy to maven repo overriding the previous maven coordinates, or deploy your thirdparty with different coordinates.
mvn deploy:deploy-file -Dfile osgify-dependency.jar ..
Enable the wrap protocol
Add to you maven karaf plugin wrap and wrapper features.
So you can use wrap protocol to fix your corrupted MANIFEST.MF
Inside some karaf features:
<bundle>wrap:mvn:group.id/third.party.artefact.id/version</bundle>
Inside your pom.xml notice feature wrap / wrapper.
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
</executions>
<configuration>
<!-- no startupFeatures -->
<bootFeatures>
<feature>feature</feature>
<feature>jaas</feature>
<feature>shell</feature>
<feature>ssh</feature>
<feature>management</feature>
<feature>bundle</feature>
<feature>config</feature>
<feature>deployer</feature>
<feature>diagnostic</feature>
<feature>instance</feature>
<feature>kar</feature>
<feature>log</feature>
<feature>package</feature>
<feature>service</feature>
<feature>system</feature>
<feature>wrap</feature>
<feature>aries-blueprint</feature>
</bootFeatures>
<installedFeatures>
..
<feature>wrapper</feature>
</installedFeatures>
</configuration>
</plugin>
Here you have the full code where i tested:
https://github.com/antoniomaria/gazpachoquest/blob/master/karaf-assembly/pom.xml
I'm new to OSGI (sorry) and having a few issues trying to deploy my package and related dependencies.
This is my POM:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.felix.test</groupId>
<artifactId>com.felix.test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.4</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
com.felix.test.search
</Export-Package>
<Bundle-SymbolicName>
${project.artifactId}
</Bundle-SymbolicName>
<Bundle-Activator>
com.felix.test.Activator
</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I'm bundling this using the Maven command:
mvn org.apache.felix:maven-bundle-plugin:bundleall
This is successful and generates my bundle as well as 3 dependency bundles:
net.sf.ehcache_2.10.0.jar
org.apache.commons.lang3_3.4.0.jar
slf4j.api_1.7.7.jar
This seems OK and I can install and start the first two but when I try and start slf4j I get the following exception:
org.osgi.framework.BundleException: Unable to resolve slf4j.api [25](R
25.0): missing requirement [slf4j.api [25](R 25.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0)) Unresolved
requirements: [[slf4j.api [25](R 25.0)] osgi.wiring.package;
(&(osgi.wiring.package=org.slf4j.impl)(version>=1.6.0))]
I'm pretty sure I'm missing something very simple but can't pin it down. Any help would be much appreciated!
Slf4j has an unusual design (some might say a bad design, ahem). It is an API bundle that depends on an implementation package, namely org.slf4j.impl.
You need to install an additional bundle that implements the Slf4j API. There are lots of choices here... for example slf4j-simple is a basic implementation, whereas slf4j-jdk14 uses the Java 1.4 java.util.logging back end, etc.
Logback also contains an implementation of the API.
Need to correct myself as slf4j indeed provides bundles now as Neil pointed out. Not sure how well it works though. I found some explanation how to install slf4j for OSGi here. It does not look very clean to me though. You need to create a bundle fragment for the configuration. Which means you can not change it at runtime.
So I still would rather recommend to use pax-logging at runtime instead. It implements the slf4j api as well as other logging APIs. As backend it uses log4j and configures it via config admin. So you do not need hacks for the logging config and can change it at runtime.
I had this same error message, and found out that it was due to bad bundle plugin configuration (as mentioned by #Christian_Schneider).
My situation
So i had roughly this error message when deploying on Karaf:
missing requirement ... Unresolved requirements: ... osgi.wiring.package=org.slf4j.impl
My modules pom.xml looked like this:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j_version}</version>
<scope>provided</scope>
</dependency>
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Private-Package>*</Private-Package>
</instructions>
</configuration>
</plugin>
Solution
credits #jbonofre from the karaf community
I just repaced <Private-Package>*</Private-Package> with <Export-Package></Export-Package>, to get:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Export-Package></Export-Package>
</instructions>
</configuration>
</plugin>
and consequently the import of org.slf4j.impl in my bundles manifest.mf disappeared, leaving only an import to org.slf4j. This is provided by PAX Logging, which is installed by default on karaf.
Something has gone wrong with my pom and I have a blank dependency and artifactId in the xml but get the same error whether I delete the tags or not.
I am using eclipse with m2eclipse installed in my home folder on Linux Mint 14 with maven version 'Apache Maven 2.2.1 (rdebian-8)' installed
Here is the pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TransferHandler</groupId>
<artifactId>TransferHandler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.4.6-rc1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.49</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.2</version>
<classifier>ftp</classifier>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<artifactId></artifactId>
</dependency>
</dependencies>
</project>
There are three error messages displayed:
On line1:
Multiple annotations found at this line:
- null (org.apache.maven.plugins:maven-resources-plugin:2.5:resources:default-resources:process-resources)
- null (org.apache.maven.plugins:maven-resources-plugin:2.5:testResources:default-testResources:process-test-
resources)
On line70:
Multiple annotations found at this line:
- Project build error: 'dependencies.dependency.groupId' for null::jar is
missing.
- Project build error: 'dependencies.dependency.version' for null::jar is
missing.
On line 71:
Project build error: 'dependencies.dependency.artifactId' for null::jar is missing.
It seems like the one on line 70 with the empty tags is the root cause (excerpt below) but I'm not sure.
<dependency>
<artifactId></artifactId>
</dependency>
Even if I delete the empty tags from the xml and refresh maven and the project the error remains. On my Dependencies tab I see a jar with a '?' next to it but can't delete it and in my Dependency Hierachy tab I see a ' : [compile]' jar but can delete or exclude it.
How can I remove this ghost/null jar?
Try This Header
<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">
Good Luck !
I found a way around the problem using git but was not able to solve it directly by removing the offending packages.
The work-around was to shutdown eclipse, remove the pom and then use git to discard changes in the working directory. Commands:
rm pom.xml
git checkout -- pom.xml
Then the pom should reappear returned to its state at the last commit (which luckily for me was before this error arose). Not a very satisfying answer but at least it worked. If anyone posts an answer and explanation to the heart of the real problem (ie why can't I remove a null package and how did it get there in the first place) I would still be happy to accept it.
I'm trying to build my OSGI bundle with pax-maven-build and in the same time test it with pax-exam. It have some bundle in provision than I can test with the following pax-exam test configuration:
#RunWith(JUnit4TestRunner.class)
#ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class OSGILoaderTest {
#Inject
protected BundleContext bundleContext;
#Configuration
public Option[] config() throws MalformedURLException {
String projectRoot = // a path to my project
return options(
junitBundles(),
equinox(),
bundle(projectRoot + "libs/org.eclipse.core.variables_3.2.500.v20110511.jar"),
bundle(projectRoot + "libs/org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar"),
bundle(projectRoot + "libs/org.eclipse.core.expressions_3.4.300.v20110228.jar"),
// etc...
);
}
#Test
public void getBundleContext() throws RodinDBException {
IRodinDB rodinDB = RodinCore.getRodinDB();
assertNotNull(rodinDB);
}
}
Here, I can see I can access to the IRodinDB instance from a jar I have provisonned.
Now I have code my own bundle, which is going to use all the jar provisionned. But I cannot even test my own code, for instance:
#Test
public void checkAccessToRodinDbTest() {
VTGService service = null;
assertTrue(true);
}
give an error at compilation time:
[ERROR] Failed to execute goal org.ops4j:maven-pax-plugin:1.5:testCompile (default-testCompile) : Compilation failure
[ERROR] cannot find symbol
[ERROR] symbol : class VTGService
It seems test compilation cannot see 'src/main/java', contrarly as expected by the default behavior of maven-compiler-plugin. But in my case, you can see than maven does not use the compiler plugin but instead maven-pax-plugin.
The question is: how can i test my own bundle with pax-exam ?
update1
It seems that this is a problem with recent version of maven-pax-plugin, as the basic example available in ops4j pax maven plugin (in section Using the Pax Plugin inside a POM) seems to suffer of the same problem.
update2
As requested by Dmytro, this is the pom.xml of my bundle:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<relativePath>../poms/compiled/</relativePath>
<groupId>fr.xlim.ssd.vtg.build</groupId>
<artifactId>compiled-bundle-settings</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<properties>
<bundle.symbolicName>fr.xlim.ssd.vtg.bundle</bundle.symbolicName>
<bundle.namespace>fr.xlim.ssd.vtg.bundle</bundle.namespace>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>fr.xlim.ssd.vtg</groupId>
<artifactId>fr.xlim.ssd.vtg.bundle</artifactId>
<version>0.1-SNAPSHOT</version>
<name>${bundle.symbolicName}</name>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<type>pom</type>
<groupId>${project.parent.groupId}</groupId>
<artifactId>provision</artifactId>
<optional>true</optional>
</dependency>
<!-- not needed as equinox bundle are available in provision -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_compendium</artifactId>
<optional>true</optional>
</dependency-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-inject</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-mvn</artifactId>
<version>1.3.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I am not sure it is the most elegant solution, but I created a new maven project when I can import my own bundle like in the source code of my question.
Is there an elegant way to add my own java sources directly as new bundle for test in the same Maven project ? It could be not possible (as the bundle assembly operation is done after the compilation and tests)...
I use the following setup to provision the bundle under test. When configuring the test, I provision the bundle using the reference-protocol (this is a non-standard feature of Equinox and Felix, see here):
#Configuration
public Option[] config() {
return options(
bundle("reference:file:target/classes"),
junitBundles(),
felix()
);
}
The test-cases also run when you specify knopplerfish() as the environment. I guess that is because the URL is resolved by Pax Exam, and not by the OSGi-runtime. I use the maven-bundle-plugin to build my bundles. To make this work as expected, you have to add the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<executions>
<!-- This execution makes sure that the manifest is available
when the tests are executed -->
<execution>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Otherwise the manifest will not be available when the tests are run, since by default it is generated during the package-phase.
I hope I did not forget anything - please let me know if it worked for you!
Check PaxExam docs how to configure your Maven POM with PaxExam.
Samples here