Need some help with setup to create new binding using maven/bnd

I had similar issues today, closed the IDE and stopped openHAB development :smiley:
The current situation is a bit madness, I agree.

But there are only about 20 bindings left to port, before we can write a new binding development documentation and find out what’s the best way to do things. I assume the problem will go away if you “resolve bnd” once more from the UI. The problem -> That will “break” other stuff at the moment.

I watch this issue, the table is getting smaller and smaller :smile:
I’ll wait until the migration finish.
I wanted to finish by binding before the next milestone release because new bindings/updates require a new version of openHAB.

While reading issues I found Your PR. You are trying to split parts of the core into separate bundles.
I’ve started wondering: maybe add-ons/bindings could be separated from the main bundle?
Currently, when there is a bug in binding and it gets fixed we have two options:

  • manually install that binding
  • wait till next release of openHAB

Wouldn’t it be awesome if we could create, version and build each binding separately?
This would allow to quickly deploy fixes and new features.

Currently in Paper UI in bindings You see all bindings that are already there, all You must do is just enable them. Why not list bindings/add-ons/etc that are available to download?

Take a look at nuget, npm and others. Each binding is already built, it has dependencies listed, so it could be easily downloaded.
I know that bindings heavily relay on openHAB version, so each package should have minimum openHAB version requirement. This is how NuGet works - if You have a .NET 3.5 application You won’t see packages that require .NET 4.7.
Node-RED has this type of nodes management mechanism.

I know that this is getting a bit of the topic, but this is somehow related to creating and distributing new bindings.

Hopefully this is the right place to ask my question because I already have my dev environment running without errors.

I’m looking for a way to add external jars in my binding. For example different version of org.apache.commons.net. How and where do I put them and what has to be done so that they resolve properly?

“RTFM” answer suits just fine, but point me to the right manual :slight_smile:

We already use the newest released variant of apache commons net, don’t we?

You just declare dependencies in your pom.xml file as with every other java project. Does that not work for you?

That might be the case and it was a bad example. I’m quite a beginner in these things.

I’ll do some tests and see if I get these things going.

See the documentation on how to include in libraries:

There is also a list of libraries that are available via by the include of the openHAB addons parent:

If you need any other library do ask for advice. Maybe the functionality you want is already available in a library already included and in such case if you would put in a pull request you will be asked to rewrite it to use that library. So to avoid having to redo please discuss new libraries.

1 Like

Thanks for the valuable links. I’ve read the doc behind second link but for some unknown reason missed the first one. This gets me much further.

Thanks again :+1:

I’ve also managed to get a working Eclipse environment for the Verisure binding that is WIP using the demo application and then importing the Verisure binding project in Eclipse.

The binding uses an external lib jsoup-1.11.3.jar that is present in the openhab2-addons/bundles/org.openhab.binding.verisure/lib folder:

jannegpriv in /Users/jannegpriv/git/openhab2-master/git/openhab2-addons/bundles/org.openhab.binding.verisure (4788-verisure) (12 entries, 3 hidden)
$ ll lib/
total 1248
-rw-r--r--@ 1 jannegpriv  staff  241622 May  6 22:29 gson-2.8.5.jar
-rw-r--r--  1 jannegpriv  staff  395748 May  4 21:38 jsoup-1.11.3.jar

It compiles fine in Eclipse and when commenting out the import for org.jsoup and all other rows using jsoup the binding starts up without errors and I can debug via the app/Debug OSGi button and launch PaperUI and start to configure my things.

However when trying to use the org.jsoup functionality the framework starts up with the following error:

! Failed to start bundle org.openhab.binding.verisure-2.5.0.201905072051, exception Could not resolve module: org.openhab.binding.verisure [123]
  Unresolved requirement: Import-Package: org.jsoup; version="[1.11.0,2.0.0)"

org.osgi.framework.BundleException: Could not resolve module: org.openhab.binding.verisure [123]
  Unresolved requirement: Import-Package: org.jsoup; version="[1.11.0,2.0.0)"

	at org.eclipse.osgi.container.Module.start(Module.java:447)
	at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:431)
	at aQute.launcher.Launcher.startBundles(Launcher.java:528)
	at aQute.launcher.Launcher.activate(Launcher.java:427)
	at aQute.launcher.Launcher.run(Launcher.java:306)
	at aQute.launcher.Launcher.main(Launcher.java:152)

My binding pom.xml looks like this:

$ cat pom.xml
<?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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.openhab.addons.bundles</groupId>
    <artifactId>org.openhab.addons.reactor.bundles</artifactId>
    <version>2.5.0-SNAPSHOT</version>
  </parent>

  <artifactId>org.openhab.binding.verisure</artifactId>

  <name>openHAB Add-ons :: Bundles :: Verisure Binding</name>
  
  <dependencies>
   <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.3</version>
      <scope>provided</scope>
    </dependency> 
    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.11.3</version>
      <scope>provided</scope>
    </dependency>
   </dependencies>

</project>

NOTE: The documentation has a typo for the end-tag regarding scope <scope>provided</provided>

I’ve also updated the feature.xml file in the features/openhab-addons/src/main/feature/feature.xml:

    <feature name="openhab-binding-verisure" description="Verisure Binding" version="${project.version}">
        <feature>openhab-runtime-base</feature>
        <feature>openhab-transport-http</feature>
        <bundle dependency="true">mvn:org.jsoup/jsoup/1.11.3</bundle>
        <bundle dependency="true">mvn:com.google.gson/gson/2.8.5</bundle>
        <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.verisure/${project.version}</bundle>
    </feature>

I would also like to use a newer version of gson (2.8.5) that I also have included, since I get a parsing error for a json when using the OH version compared to a local test using gson-2.8.5.jar.

What am I missing?

At the moment we have not finalized the ide setup for the new buildsystem. The demo app is not using karaf so editing that file will not help to find the library at runtime.

Instead you want to go to the bndrun file editor and click on resolve. It should add the poms.xml defined dependencies to the bndrun file. That’s your runtime environment file at the moment, not the karaf feature file.

Cheers David

No I seem to have ended up in the same error as @Misiu had above:

Status ERROR: bndtools.core code=0 Cannot find /error/org.openhab.binding.verisure;version=0 Not found in [bndtools.m2e.MavenImplicitProjectRepository@3999a433, bnd-cache [/Users/jannegpriv/.bnd/default-ws/cnf/cache/4.2.0/bnd-cache r/w=false], org.openhab.demo.app [/Users/jannegpriv/git/openhab2-master/git/openhab-demo/app/target/tmp/resolve/app/cnf/cache/4.2.0/org.openhab.demo.app r/w=false]] null
Status ERROR: bndtools.core code=0 org.openhab.binding.verisure;version=0 Not found in [bndtools.m2e.MavenImplicitProjectRepository@3999a433, bnd-cache [/Users/jannegpriv/.bnd/default-ws/cnf/cache/4.2.0/bnd-cache r/w=false], org.openhab.demo.app [/Users/jannegpriv/git/openhab2-master/git/openhab-demo/app/target/tmp/resolve/app/cnf/cache/4.2.0/org.openhab.demo.app r/w=false]] null

probably after I pressed the Resolve button.
Maybe I need to set up the environment from scratch again or is there a way to fix this?

It should add the poms.xml defined dependencies to the bndrun file.

Where is the bndrun file located?

After setting up the demo app again I now have a functional IDE environment again.
However I still cannot get the runtime environment to resolve my added dependency library
jsoup-1.11.3.jar.

The jar-file is present in the lib folder of my binding:
openhab2-addons/bundles/org.openhab.binding.verisure/lib

and the verisure bindings pom.xml looks like this:

jannegpriv in /Users/jannegpriv/git/openhab2-master/git/openhab2-addons/bundles/org.openhab.binding.verisure (4788-verisure) (12 entries, 3 hidden)
$ cat pom.xml
<?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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.openhab.addons.bundles</groupId>
    <artifactId>org.openhab.addons.reactor.bundles</artifactId>
    <version>2.5.0-SNAPSHOT</version>
  </parent>

  <artifactId>org.openhab.binding.verisure</artifactId>

  <name>openHAB Add-ons :: Bundles :: Verisure Binding</name>
  
  <dependencies>
    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.11.3</version>
      <scope>provided</scope>
    </dependency>
   </dependencies>
  
</project>

When I press the Resolve button the jsoup lib is not listed as an Optional Resource. I have a number of jsoup.jar files downloaded in my .m2 folder:

$ cd jsoup/
jannegpriv in /Users/jannegpriv/.m2/repository/org/jsoup/jsoup  (8 entries, 0 hidden)
$ ll
total 0
drwxr-xr-x  10 jannegpriv  staff  320 May  8 22:31 1.10.1
drwxr-xr-x   5 jannegpriv  staff  160 Sep 25  2018 1.10.2
drwxr-xr-x  10 jannegpriv  staff  320 May  8 22:29 1.10.3
drwxr-xr-x   9 jannegpriv  staff  288 Nov  5  2018 1.11.2
drwxr-xr-x  10 jannegpriv  staff  320 May  4 23:50 1.11.3
drwxr-xr-x   7 jannegpriv  staff  224 Sep 25  2018 1.7.1
drwxr-xr-x   7 jannegpriv  staff  224 Aug 30  2018 1.7.2
drwxr-xr-x  10 jannegpriv  staff  320 May  8 22:29 1.8.3

Error when pressing Run OSGi

:16:56.158 [main] WARN  o.o.p.w.u.ServletContainerInitializerScanner:136 - failed to parse and instantiate of javax.servlet.ServletContainerInitializer in classpath
! Failed to start bundle org.openhab.binding.verisure-2.5.0.201905132101, exception Could not resolve module: org.openhab.binding.verisure [121]
  Unresolved requirement: Import-Package: org.jsoup; version="[1.11.0,2.0.0)"

org.osgi.framework.BundleException: Could not resolve module: org.openhab.binding.verisure [121]
  Unresolved requirement: Import-Package: org.jsoup; version="[1.11.0,2.0.0)"

	at org.eclipse.osgi.container.Module.start(Module.java:447)
	at org.eclipse.osgi.internal.framework.EquinoxBundle.start(EquinoxBundle.java:431)
	at aQute.launcher.Launcher.startBundles(Launcher.java:528)
	at aQute.launcher.Launcher.activate(Launcher.java:427)
	at aQute.launcher.Launcher.run(Launcher.java:306)
	at aQute.launcher.Launcher.main(Launcher.java:152)

Any tips of what might be wrong or missing are appreciated! :slight_smile:

The buildsystem section of the doc might help. Just add the dependency to the lib directory of your binding for the time being.

Usually you would list it as runtime dependency in your features.xml file. But for development that doesn’t really work.

I tried to follow the instructions in the buildsystem documentation and I’ve added the dependency to the jsoup jar in the bindings pom.xml as shown above but what exactly does this mean:

Just add the dependency to the lib directory of your binding for the time being.

How do I accomplish that?
Sorry for the maybe newbie questions but I’m really eager to get this working so that I can continue with the binding implementation :slight_smile:

Hello,
I add the library in this window.
On your binding folder, right button and select properties

1 Like

But be sure that the external library is really in the “lib” directory of your bundle (you need to create that directory). Like on the screenshot.

I have the jsoup library in the lib foider of my binding and my Java Build Path has the library listed:

jannegpriv in /Users/jannegpriv/git/openhab2-master/git/openhab2-addons/bundles/org.openhab.binding.verisure (4788-verisure) (11 entries, 3 hidden)
$ ll
total 72
-rw-r--r--   1 jannegpriv  staff    495 May  4 21:38 NOTICE
-rw-r--r--   1 jannegpriv  staff  23857 May  4 21:38 README.md
drwxr-xr-x   5 jannegpriv  staff    160 May  4 20:58 bin
drwxr-xr-x   4 jannegpriv  staff    128 May  6 22:29 lib
-rw-r--r--   1 jannegpriv  staff    797 May 13 23:00 pom.xml
-rw-r--r--   1 jannegpriv  staff    973 May  7 23:51 pom.xml.bak
drwxr-xr-x   4 jannegpriv  staff    128 May  5 00:31 src
drwxr-xr-x  13 jannegpriv  staff    416 May  8 23:12 target
jannegpriv in /Users/jannegpriv/git/openhab2-master/git/openhab2-addons/bundles/org.openhab.binding.verisure (4788-verisure) (11 entries, 3 hidden)
$ ls -l lib/
total 1248
-rw-r--r--@ 1 jannegpriv  staff  241622 May  6 22:29 gson-2.8.5.jar
-rw-r--r--  1 jannegpriv  staff  395748 May  4 21:38 jsoup-1.11.3.jar

gson-2.8.5.jar is also present in the lib folder but it is also part of the demo.app so I guess that is why I don’t get any problems with resolving gson library at runtime.

So it is not a problem at compile time, only at runtime when either using the Run or Debug OSGi buttons.
If I comment out my imports of org.jsoup.* I can start and debug my binding without any problem.

However since I’ve used jsoup to parse HTML files before moving to the new bnd environment, I would really like the possibility to use jsoup in my further binding development. I could maybe use String/StringBuilder to accompish the same logic, however the code will be so much more hard to understand.

Can I manually add it to the openhab-demo/app/app.bndrun file?
In that case under which part? runbundles or runrequires?

Another thing I’ve noticed is that jsoup is used by 2 official bindings:

$ find . -type f|xargs grep -l jsoup
./org.openhab.binding.kostalinverter/target/classes/org/openhab/binding/internal/kostal/inverter/firstgeneration/WebscrapeHandler.class
./org.openhab.binding.kostalinverter/target/classes/META-INF/MANIFEST.MF
./org.openhab.binding.kostalinverter/pom.xml
./org.openhab.binding.kostalinverter/NOTICE
./org.openhab.binding.kostalinverter/src/main/java/org/openhab/binding/internal/kostal/inverter/firstgeneration/WebscrapeHandler.java
./org.openhab.binding.zoneminder/target/classes/NOTICE
./org.openhab.binding.zoneminder/pom.xml
./org.openhab.binding.zoneminder/NOTICE
./org.openhab.binding.verisure/bin/target/MANIFEST.MF
./org.openhab.binding.verisure/bin/target/org.openhab.binding.verisure-2.5.0-SNAPSHOT.jar
./org.openhab.binding.verisure/bin/target/code-analysis/spotbugsXml.xml
./org.openhab.binding.verisure/target/com.googlecode.addjars.mojo.AddJarsMojo/target/code-analysis/spotbugsXml.xml
./org.openhab.binding.verisure/target/com.googlecode.addjars.mojo.AddJarsMojo/pom.xml
./org.openhab.binding.verisure/target/classes/org/openhab/binding/verisure/internal/VerisureSession.class
./org.openhab.binding.verisure/target/classes/META-INF/MANIFEST.MF
./org.openhab.binding.verisure/target/classes/NOTICE
./org.openhab.binding.verisure/pom.xml.bak
./org.openhab.binding.verisure/pom.xml
./org.openhab.binding.verisure/.classpath
./org.openhab.binding.verisure/NOTICE
./org.openhab.binding.verisure/lib/jsoup-1.11.3.jar
./org.openhab.binding.verisure/src/main/java/org/openhab/binding/verisure/internal/VerisureSession.java
./org.openhab.transform.jinja/NOTICE

However none of those binding have the jsoup library in respective bindings lib folder. They also refer to 2 different versions of jsoup library. How can those bindings resolve jsoup at runtime?

Via the features.xml file. We do not encourage people to put libs into the lib dir. I suggested that only for you to keep going with your setup.

In short:

  • For openhab use the features.xml for
  • For local development use the libs dir or edit the bndrun file. The latter is preferred but requires a bit more knowledge.

For local development use the libs dir or edit the bndrun file. The latter is preferred but requires a bit more knowledge.

I tried to add the jsoup file to the bndrun file but it then complained about not being able to resolve the jsop jar, I then tried to copy it to a ~/.bnd/…/…/…/…/cache directory but that did not work either.
Unfortunately I’m stuck in getting an external library to work in my local development environment.

Does external libraries work in other bnd converted local development environments? Has anyone succeeded in resolving an external library on your local bnd Eclipse environment?

Usually:

  1. Add the maven dependency via pom.xml
  2. Run maven with -DwithResolver or in Eclipse let maven refresh (project right click->maven->refresh or something) click the “resolve” button in the bnd window.

I’m really thankful for for help but I’ve tested everything and Eclipse can’t still resolve the jsoup library at runtime (when starting via Run OSGi/Debug OSGi). So frustrating.