bnd gradle - bundle/jar and its req'd dependencies

586 views
Skip to first unread message

Sko

unread,
Mar 24, 2016, 12:18:28 PM3/24/16
to bndtools-users
Hi,

I have a OSGi project that uses gradle with bnd plugin - so far so good, but now I would like to produce a jar to be used as CLI tool (to be run from command line) that depends on other bundles.
Obviously, to be able to run the jar I need to provide all its dependencies. And here is the problem - how can I get the list of required bundles and their dependencies?
I mean, to get the list from bnd/gradle somehow so I could then create a distributable jar along with all required libs.

Simplified structure of my bnd driven project:
- subsystem.helpers (depends on 3rd party libs etc)
- subsystem.protocol (depends on 3rd party libs etc)
- subsystem.client (depends on subsystem.helpers & subsystem.protocol)
- subsystem.cli.tool (depends on subsystem.client and 3rd party libs)

I would be grateful for any input.

Regards,
Piotr

Przemysław Wesołek

unread,
Mar 25, 2016, 6:24:51 AM3/25/16
to bndtools-users
I'm not sure if I understand correctly, but take a look at runbundles.* and export.* sets of Gradle tasks. The first one will generate a set of all runtime dependencies based on bndrun files found in your projects. The second one goes a step further and packs all those dependencies into one "fat JAR", adding a launcher, which allows you to run your application as simply as java -jar fatjar.jar.

Regards,
Przemek

Sko

unread,
Mar 25, 2016, 8:24:26 AM3/25/16
to bndtools-users
I don't have bndrun configurations and what's more I would like not to have them, especially for the questioned CLI tool.

The reasons are:
- CLI tool does not require OSGi environment - it is a plain java command line class using some classes from other bundles
- from what I see, in bndrun I would need to specify dependencies - I would prefer not to double it, since it should be enough to have deps specified in bnd.bnd file.
- I would need to repackage "fatjar.jar" to remove launcher

I was rather thinging that there should be a way to get the list of dependant libs and I could repackage them into one single jar.

Sko

Peter Kriens

unread,
Mar 25, 2016, 8:32:08 AM3/25/16
to bndtool...@googlegroups.com
I was rather thinging that there should be a way to get the list of dependant libs and I could repackage them into one single jar.
This is exactly what the Fat Jar is … It is an executable JAR with the dependencies.

I am confused about your mail. If you’re not using OSGi in your tool, why use Bndtools?

Kind regards,

Peter Kriens


--
You received this message because you are subscribed to the Google Groups "bndtools-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bndtools-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sko

unread,
Mar 25, 2016, 8:50:29 AM3/25/16
to bndtools-users

> This is exactly what the Fat Jar is … It is an executable JAR with the dependencies.

FatJar as made by export command has embedded launcher that starts aQute OSGi framework and then runs the bundle - this is something I would like to avoid.

> I am confused about your mail. If you’re not using OSGi in your tool, why use Bndtools?

Everything else in the project is OSGi, the thing is that I need to create two plain-old jars (+ have their dependencies) to:
- have a CLI tool
- have a simple Test scenario that can be run on jmeter

Of course, I could build CLI tool and the jmeter plugin using Java Plugin of Gradle w/o using bndtools, but then I would have a similar problem - I need to feed the gradle with (bnd originating) dependencies to build the classes.

Regards,
Sko

Peter Kriens

unread,
Mar 25, 2016, 12:52:11 PM3/25/16
to bndtool...@googlegroups.com
Ok, I understand.

I that case create a project (or a sub bundle). Make sure that you add to the Private Packages all the classes that you need. You can check the Contents tab that you have only imports from the JVM. (Conditional Package might help here, see http://enroute.osgi.org/tutorial_wrap/210-dependencies.html)

Then add `Main-Class: <your main class with static void main(String[] args) method>` to the bnd file.

This bundle is now an executable JAR. You can run it with `java -jar generated/com.example.foo.jar`

com.example.foo.Main:

package com.example.foo;

public class  Main {
public static void main(String args[]) {
System.out.println(“Hello World”);
}
}

bnd.bnd:

Main-Class: com.example.foo.Main
Conditional-Package: \
!javax.*, \
!org.omg.*, \
!org.w3c.*, \
!org.sax.*, \
*

-buildpath: \
com.example.other.project, \
com.otext.bar


Kind regards,

Peter Kriens


Sko

unread,
Mar 31, 2016, 8:57:13 AM3/31/16
to bndtools-users
That did the job. I was blinded by an idea of using gradle to create fatjar and didn't investigate path where bnd could do that.
Thanks!
Reply all
Reply to author
Forward
0 new messages